Variable Declarations in Header Files - Static or Not

Variable declarations in header files - static or not?

The static means that there will be one copy of VAL created for each source file it is included in. But it also means that multiple inclusions will not result in multiple definitions of VAL that will collide at link time. In C, without the static you would need to ensure that only one source file defined VAL while the other source files declared it extern. Usually one would do this by defining it (possibly with an initializer) in a source file and put the extern declaration in a header file.

static variables at global level are only visible in their own source file whether they got there via an include or were in the main file.


Editor's note: In C++, const objects with neither the static nor extern keywords in their declaration are implicitly static.

Static const variable declaration in a header file

I answered this at length here. That answer is for C++, but it holds true for C as well.

The translation unit is the individual source file. Each translation unit including your header will "see" a static const int. The static, in this context, means the scope of my_variable is limited to the translation unit. So you end up with a separate my_variable for each translation unit (".c file").

The compiler would not be "smart" to create only one instance for all files, it would be faulty, because you explicitly told it not to do so (static).

Variable declaration in a header file

You should declare the variable in a header file:

extern int x;

and then define it in one C file:

int x;

In C, the difference between a definition and a declaration is that the definition reserves space for the variable, whereas the declaration merely introduces the variable into the symbol table (and will cause the linker to go looking for it when it comes to link time).

Static storage class in header file

You can declare static variable in header files but this variable scope will only be that *.c file in which this header file will be included.

Use of variable in header files, duplicate definition error

Header guards won't help here, they address only the problem of including the header file more than once in the same compilation unit (AKA .c file).

Here you define a non static variable in a header file. If you do this, all .c files that include this header file will compile cleanly, but during the linking process all these variables will be multiply defined. It's just like if you'd put a function definition into a header file.

You should either have this in your header file:

static const uint16_t GPIO_SWPVM_LEDS[12] = {......};

...or have this in the header file (which is much is much cleaner, because the const variable will exist only once):

extern const int GPIO_SWPVM_LEDS[12];

and put the variable definiton in one of your .c files:

const uint16_t GPIO_SWPVM_LEDS[12] = {......};

Read this SO article for more information:

  • How do I use extern to share variables between source files?

Where should static variables be declared

You declare the variables inside the class definition, then you define them in a source file.

But you need to use the proper scope when defining the variable, just like you use for member functions:

const int File::number = 10;
// ^^^^^^
// Note scope here

Declare and define static variable in C++ header?

You can abuse the singleton pattern if you really must avoid any .cpp files:

class Foo {
public:
static Bar& getMyStatic() {
static Bar bar;
return bar;
};
};

This works because now the variable is a static variable inside a function, and static has a different meaning within a function context than within a class context. And for functions, the linker does recognize multiple identical definitions and throws away the copies.

But, of course, I would strongly advise against avoiding .cpp files: It means that you get into a situation where you have to build the entire program, or at least large parts of it, in one big piece. Every change you do will necessitate a complete rebuilt which slows down your change-compile-test cycle significantly. For very small projects that might not be a problem, but it is for medium to large ones.

Okay to declare static global variable in .h file?

If you write

static const int x

in an .h file then every translation unit that #include-s this .h will have its own private variable x.

If you want to have 1 global variable visible to everyone you should write

extern const int x;

in the .h file and

const int x = ...;

in one of the .cpp files.

If you want to have a static const int visible to just one translation unit - don't mention it in the .h files at all.



Related Topics



Leave a reply



Submit