share a variable across two libraries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • guddu

    share a variable across two libraries

    Hi,
    I have to share a variable across two libraries. These libraries are
    build separately and share a common header file.

    I had tried this earlier using extern variable. That didn't help.

    Any ideas about this ??

    Thanks and Regards,
  • Richard Heathfield

    #2
    Re: share a variable across two libraries

    guddu said:
    Hi,
    I have to share a variable across two libraries. These libraries are
    build separately and share a common header file.
    >
    I had tried this earlier using extern variable. That didn't help.
    >
    Any ideas about this ??
    If you must do this (rather than use parameter-passing, say), do it like
    this:

    /* commonheader.h */
    #ifndef H_COMMONHEADER
    #define H_COMMONHEADER 1
    extern int shared_object; /* a declaration - include as appropriate */
    /* + whatever else you feel appropriate in here */
    #endif

    /* firstlibsourcef ile.c */
    #include "commonheader.h "
    int shared_object; /* ONE definition, here ONLY */
    /* functions and stuff go here */

    /* someotherlibsou rcefile.c */
    #include "commonheader.h " /* get the declaration */
    /* Note the absence of a definition */
    /* functions and stuff go here */

    HTH. HAND.


    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • rahul

      #3
      Re: share a variable across two libraries

      On Jun 24, 4:14 pm, guddu <visha...@gmail .comwrote:
      Hi,
      I have to share a variable across two libraries. These libraries are
      build separately and share a common header file.
      >
      I had tried this earlier using extern variable. That didn't help.
      How it didn't help?
      Any ideas about this ??
      If you want to share data, use parameters. Globals are not a good idea
      except in very specific situations. Nevertheless, it works.


      Comment

      Working...