• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

extern variables

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 :
***************file1.h********************************************
int i = 1;
void runme(void);

***************file2.C********************************************
include "file1.h"

runme()
{
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?
like
CC -G file2.C -o libfile2.so
CC file3.C -lfile2 -o a.out

Thanks

0
rats54
Asked:
rats54
1 Solution
 
PaulCaswellCommented:
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.

Paul
0
 
rats54Author Commented:
Any reason why it doesnt clash when it is in the library?..... must be some reason why it behaves differently.....?
0
 
KurtVonCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
brettmjohnsonCommented:
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

0
 
PaulCaswellCommented:
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.

Paul
0
 
rats54Author Commented:
Kurtvon.....in 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?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now