How do I share a global variable between c files?
file 1: int x = 50; file 2: extern int x; printf(“%d”, x);
file 1: int x = 50; file 2: extern int x; printf(“%d”, x);
You should only use extern template to force the compiler to not instantiate a template when you know that it will be instantiated somewhere else. It is used to reduce compile time and object file size. For example: // header.h template<typename T> void ReallyBigFunction() { // Body } // source1.cpp #include “header.h” void something1() { … Read more
The common practice is not to demand client code wraps your header in extern “C”, but to do so conditionally yourself. For instance: #ifdef __cplusplus extern “C” { #endif // Header content #ifdef __cplusplus } #endif That way client code is automatically correct without doing anything beyond including the header.
From The C Book: If a declaration contains the extern storage class specifier, or is the declaration of a function with no storage class specifier (or both), then: If there is already a visible declaration of that identifier with file scope, the resulting linkage is the same as that of the visible declaration; otherwise the … Read more
extern gives a name external linkage. This means that the object or function is accessible through this name from other translation units in the program. For functions, this is the default linkage in any case so its usage (in this context) is usually redundant.
1) It may not have a header file. But yes, in general, for large projects, you should have a header file if multiple translation units are going to use that function (don’t repeat yourself). 2) The linker searches through all the object files and libraries it was told about to find functions and other symbols.
There’s surprisingly little in the standard about this. About all we hear about redeclaration is: [C++11: 3.1/1]: A declaration (Clause 7) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations. [..] and the only relevant part of auto‘s semantics: [C++11: 7.1.6.4/3]: Otherwise, the type of the variable … Read more
The whole and entire purpose of static is to declare that a variable is private to the source file that it is declared in. Thus, it is doing precisely its job in preventing a connection from an extern. Keep in mind that there are four flavors of file-scope variable definition: int blah = 0; — … Read more
Yes, you can use them together. And yes, it should exactly match the declaration in the translation unit it’s actually declared in. Unless of course you are participating in the Underhanded C Programming Contest 🙂 The usual pattern is: file.h: extern const int a_global_var; file.c: #include “file.h” const int a_global_var = /* some const expression … Read more
Consider reading section 10.6.7 of the C# specification, which answers many of your questions. I reproduce part of it here for your convenience: When a method declaration includes an extern modifier, that method is said to be an external method. External methods are implemented externally, typically using a language other than C#. Because an external … Read more