constant variables not working in header constant variables not working in header c c

constant variables not working in header


The problem is that you define objects with external linkage in header file. Expectedly, once you include that header file into multiple translation units, you'll get multiple definitions of the same object with external linkage, which is an error.

The proper way to do it depends on your intent.

  1. You can put your definitions into the header file, but make sure that they have internal linkage.

    In C that would require an explicit static

    static const double PI = 3.1415926535; static const double PI_under_180 = 180.0f / PI; static const double PI_over_180 = PI/180.0f; 

    In C++ static is optional (because in C++ const objects have internal linkage by default)

    const double PI = 3.1415926535; const double PI_under_180 = 180.0f / PI; const double PI_over_180 = PI/180.0f; 
  2. Or you can put mere non-defining declarations into the header file and put the definitions into one (and only one) implementation file

    The declarations in the header file must include an explicit extern and no initializer

    extern const double PI; extern const double PI_under_180; extern const double PI_over_180; 

    and definitions in one implementation file should look as follows

    const double PI = 3.1415926535; const double PI_under_180 = 180.0f / PI; const double PI_over_180 = PI/180.0f; 

    (explicit extern in the definitions is optional, if the above declarations precede the definitions in the same translation unit).

Which method you will choose depends on your intent.

The first method makes it easier for the compiler to optimize the code, since it can see the actual value of the constant in each translation unit. But at the same time conceptually you get separate, independent constant objects in every translation unit. For example, &PI will evaluate to a different address in each translation unit.

The second method creates truly global constants, i.e. unique constant objects that are shared by the entire program. For example, &PI will evaluate to the same address in each translation unit. But in this case the compiler can only see the actual values in one and only one translation unit, which might impede optimizations.


Starting from C++17 you get the third option, which sort of combines "the best of both worlds": inline variables. Inline variables can be safely defined in header files despite having external linkage

inline extern const double PI = 3.1415926535; inline extern const double PI_under_180 = 180.0f / PI; inline extern const double PI_over_180 = PI/180.0f; 

In this case you get a named constant object whose initializer value is visible in all translation units. And at the same time the object has external linkage, i.e. it has a global address identity (&PI is the same in all translation units).

Granted, something like that might only be necessary for some exotic purposes (most use cases in C++ call for the first variant), but the feature is there.


extern means the 'real' definition of the variable is elsewhere, and the compiler should trust that things will hook up at link time. Having the definition inline with the extern is weird and is what's munging up your program. If you want to have them be extern, just define them exactly once elsewhere in your program.


The extern storage class for them is almost certainly the cause of the problem you're seeing. If you remove it, the code will probably be fine (at least in this respect).

Edit: I just noticed that you've tagged this as both C and C++. In this respect C and C++ are really quite different (but from the error messages, you're apparently compiling as C++, not C). In C++, you want to remove the extern, because (by default) const variables have the static storage class. That means each source file (translation unit) will get its own "copy" of the variable, and there won't be any conflict between definitions in different files. Since you're (probably) only using the values, not treating them as variables, having multiple "copies" won't hurt anything -- none of them will be allocated storage space.

In C, extern is rather different, and removing the extern won't make any real difference, because they'll be extern by default. In this case, you really need to initialize the variables in exactly one place, and declare them extern in the header. Alternatively, you can add the static storage class that C++ will add by default when/if you remove the extern from the header.