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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.