extern variables

Posted on 2005-05-09
Last Modified: 2010-04-02
I was reading some old code and came across this comment.

"/* Declaring the variable in the header is a bit of a dirty hack, but it's
 * required because the my library is used by more than one executable,
 * and hence an extern may not always do the trick to resolve the symbols.
I m trying to understand when would extern not be able to resolve symbols...any ideas?

Secondly I have a code in 3 files :
int i = 1;
void runme(void);

include "file1.h"

i = 10;
cout << i << endl;

***************file3.C *************************************************

include "file1.h"
main ()
i = 5;
cout << i << endl;

Now when I compile as CC file2.C file3.C i get error for multiple declarations which if as expected.
But when I create a library (.so/.a) from file2.C and the link it with file3 to create executable i donot get error for multiple declarations. Why is that so?
CC -G file2.C -o
CC file3.C -lfile2 -o a.out


Question by:rats54
    LVL 16

    Accepted Solution

    This is a common confusion with libraries. When you link the library into your executable, the 'int i' declared in file1.h in file2.c overwrites the second one declared in file1.h in file1.c. When they are linked as object files the two 'i's clash as duplicates.

    The usual way to do this is to extern the 'i' in file1.h and declare it in file2. Then, whether its linked as a library or an object file it will still work.

    Offhand, I canty think of a valid reason for doing it the way you have seen. I'll have to think about it.


    Author Comment

    Any reason why it doesnt clash when it is in the library?..... must be some reason why it behaves differently.....?
    LVL 11

    Expert Comment

    The reason the library works is because there are two "i" variables there.

    Basically, every time a file includes file1.h it will create a global variable called i.  When both files are compiled together into a single program, it tries to create two gloabl variables called i and fails.  No surprise there.

    When the library is made, it creates a global variable called i, but then it is compiled into object code, and the name of that global variable is no longer important.  Since it is not an "extern" it is not exposed outside the library and the name is "lost" at compile time.  Then the other file is compiled, creates its own version of i and then happily links in the library with the unnamed "i".  So the two "global" versions of i are actually two different variables.

    As to why someone would do this, I'm not sure.  The only explanation I can come up with is they wanted a global variable associated with the main program, but then didn't want it associated with the library, which should have its own global version.  That really does sound like a genuine hack, though.

    Hope this helps.
    LVL 23

    Expert Comment

    The old code is pre-ansi, and relies on common data: where multiple definitions
    of the same global variable results in a single instance.  

    You will also get compiler warnings because the declaration of runme in file1.h
    does not match the definition in file2.c

    LVL 16

    Expert Comment

    It is possible with some linkers to merge duplicate declarations and allow one to overwrite the other. This is one way of modifying library functionality.

    I have used this technique under DOS when I need to override the limit to the number of open files. There is a global variable you can change at run-time that changes the limit but the file handles are stored in an array of fixed length in a library. The only way to fully remove the limit is to redefine that array too and get the linker to use your one instead of the one in the library.

    I still dont see how using 'int i;' in a header will help achieve anything predictable.


    Author Comment

    by:rats54 case of the library being linked in , there is only one version of variable (i think) coz i get same value of variable "i" in the cout. Had it been two different variables I couldnt have one value...rt?

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now