简体   繁体   中英

How to have a global variable in C++

I'm rewriting someone's code who had a global variable that is useful during initialization when Java makes it's callbacks into C, and just after. It appears they have declared this variable in several places with an extern prefix and once without. I don't really see why they would do this to a global variable (prefixed g_ ) when I would rather call it static and declare it just once.

Would I be able to do this or does static have negative connotations for thread safety in this context? The bulk of my code is C++ but there is some extern 'C' stuff like those functions Java calls into.

Currently I'm not using extern or static but I'm inclined to believe this is why I'm getting linking errors.

static does not prevent a variable from being duplicated during the compilation of multiple compilation units. When you link those compilation units, they will end up seeing different "instances" of the same variable. In other words, each one will see its own copy.

The usefulness of extern is exactly to avoid this duplication. You declare the global (non- static ) variable in an implementation file ( .c ) and declare it as extern on a given header file ( .h ) to be included by every source file that depends on it.

You can have global variables in C++ just like you can in C. Global variables constitute shared state, so just as in C, or in any language for that matter, if you access shared state from multiple threads concurrently (with at least one mutable access), you have to provide synchronization.

The extern "C" linkage specification is only necessary if you want to access the same variable from multiple translation units compiled in different languages that all use C linkage rules.

(Unlike in C, C++ global variables can have non-trivial initialization, which brings some new issues of ordering. But that shouldn't matter.)

You mixing extern , extern "C" and static

When declaring a regular variable or function in the global scope, the compiler expose it as a "public symbol"

when you use the extern before the variable declaration you specify it is a public symbol declared in other file. if you won't use the extern you'll get link error said the variable already defined.

The static keyword before a declaration do quite the opposite thing: it prevent the symbol from been public, therefore, it known only where it been declared (or included) and can't been extern ed. that is why you won't get the "already defined" link error when you declare the same static variable twice, and it actually will be declared twice.

and finally the extern "C" make variables been published in the C convention not in the C++ convention. the main difference is what called "overloading". in C++ several function can have the same name, with different arguments types, therefore need a convention to keep the types in the function name. in C the name as is is the public symbol name. you should use it when you wan't to make C++ function usable from C, or any other language, or maybe even from other C++ compiler version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM