Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variables in multiple c files 1

Status
Not open for further replies.

svdz

Programmer
Dec 9, 2004
4
0
0
ZA
How do I use global vaiables in c without getting multiple defintions of the same variable?
 
Place a declaration in the header file of your choice that's to be included in each of the multiple files, then add a new file for the definition:
Code:
/* foo.h */
extern int global;
Code:
/* foo.c */
int global = 0;
Include foo.h everywhere you need global, compile and link foo.c along with your other source files, and lo! Each translation unit recognizes global, yet there's only one definition. All is right with the world...until you try to access global simultaneously from multiple threads. :)

Though it's possible that you can avoid some slippery slopes by not using a global variable in the first place. Globals are seductively easy, but there are hidden pitfalls that should be taken into account before using one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top