Thread: how extern is extern?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    61

    how extern is extern?

    Hello,

    i have an event driven c library that needs to notify the thing it links with that a status has changed. i cant call a function because obviously it would be present at compile time of the library.

    i also defined an extern int and then defined int in the executable that links with the library but it created its own copy; it couldnt see the extern int from the library.

    my question is can i define an int (it just needs to be a flag) that has a larger scope than one compile sesh i.e. something as visible as a function (i know how bad that sounds )

    thanks for your help

    PS: is there another way i could pass info from a library to an executable that is linked with it? (ideally i would like to have a function in the 'client' executable that is called by the library)

    all the functions i used to call the library functions have exited by then btw

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What platform is this?

    In general, DLL's and Shared libraries do not share variables with the executable. If you need to do that, you will need to have some sort of interface via function calls.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    so like a polling loop that asks the library every x interval?

    thats kinda hacky...

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    sorry, the platform is iPhone

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't know much about iPhone, but the general method you do this with is that you pass a pointer/reference from the main executable to the library, or you call a function in the library to get a pointer/reference to the variable. Once you have the "handle" of the variable, you can obviously access it as you like.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    as suggested in the previous post, the easy solution is to define a function in the lib that returns a reference to your integer, another idea to let your lib notify the app is to make the app pass a callback to the library; otherwise, if the iphone has any equivalent of signals, the lib could also raise one despite this is not such a great solution.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Like root4 was saying, use function pointers so your library can be passed a callback function to call when the event occurs
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by davo666 View Post
    my question is can i define an int (it just needs to be a flag) that has a larger scope than one compile sesh i.e. something as visible as a function (i know how bad that sounds )
    You can do that, though it's not recommended. As suggested before, you should use a function to access that variable. But if you must, here is how to do it.
    In your library, in a .c file, declare your variable at filescope, i.e. not within a function.
    Code:
    #include <blah.h>
    
    int flag = 0;
    
    void blah_func (void) {
        /** Just to illustrate **/
    }
    In another file, you can then use that variable by doing the same thing as above, but putting extern in front of the variable declaration:
    Code:
    #include <blah_lib.h>
    
    extern int flag = 0;
    
    void some_func (void) {
        /** Just to illustrate **/
    }
    Both variables should then refer to the same memory location and thus have the same value for both. Important is not to declare it extern in the lib and not to put it into header files!

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by QuantumPete View Post
    In your library, in a .c file, declare your variable at filescope, i.e. not within a function.

    [...]

    In another file, you can then use that variable by doing the same thing as above, but putting extern in front of the variable declaration:
    I doubt that this will work with dynamic linking, because the compiler doesn't know the address of flag at compile time. Furthermore, if flag never appears as an lvalue or rvalue, the compiler might optimize it away.

    I haven't ever tried though, because the natural way is to encapsulate the data in functions (think OO):

    Code:
    static int flag;
    
    void flag_set(int x) { flag = x; }
    int flag_get() { return flag; }
    For thread-safety, additionally use handles as appropriate (where handle might be anything, ranging from ints to a callback functions).

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Snafuist View Post
    I doubt that this will work with dynamic linking, because the compiler doesn't know the address of flag at compile time.
    Potentially, I've never tried it with dynamic linking tbh.

    Quote Originally Posted by Snafuist View Post
    Furthermore, if flag never appears as an lvalue or rvalue, the compiler might optimize it away.
    Well, yes. In the contrived example that I put up in my previous post, that's true. But if your lib sets the flag and your client uses it to check something, then it will most definitely be used. You need to make a distinction between small snippets to illustrate a point and a full blown program...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #11
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by QuantumPete View Post
    But if your lib sets the flag and your client uses it to check something, then it will most definitely be used.
    But not necessarily the other way around (think "volatile"), though I'm not sure. Actually, I wasn't talking about your code, just gathering (possibly poor) arguments in favor of data encapsulation.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Snafuist View Post
    But not necessarily the other way around (think "volatile"), though I'm not sure. Actually, I wasn't talking about your code, just gathering (possibly poor) arguments in favor of data encapsulation.

    Greets,
    Philip
    I'm all for data encapsulation. In fact, the code that I gave would never [ever] pass code review! I was just showing how to use extern correctly if you have a desire to make your code unreadable.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM