constant variables not working in header

C++CVisual StudioVisual Studio-2008Visual C++

C++ Problem Overview


if I define my constant varibles in my header like this...

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

I get the following error

1>MyDirectX.obj : error LNK2005: "double const PI" (?PI@@3NB) already defined in main.obj
1>MyDirectX.obj : error LNK2005: "double const PI_under_180" (?PI_under_180@@3NB) already defined in main.obj
1>MyDirectX.obj : error LNK2005: "double const PI_over_180" (?PI_over_180@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI" (?PI@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI_under_180" (?PI_under_180@@3NB) already defined in main.obj
1>MyGame.obj : error LNK2005: "double const PI_over_180" (?PI_over_180@@3NB) already defined in main.obj

but If I remove those constants from the header and put them in the document that is including the header like this...

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

It works

Does anyone have Idea what I might be doing wrong ??

Thanks

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

The problem is that you are initializing the variables in the header file; this creates a defining declaration, which is repeated in every file that includes that header,hence the multiple definition error.

You want a non-defining declaration (no initializer) in the header file, and put the defining declaration in one of the implementation files.

Solution 5 - C++

A lot of incorrect responses below. The ones that are correct are the ones telling you to remove the extern as sellibitze has also said in his comment are correct.

Because these are declared const, there is no problem having the definition in the header. C++ will inline a const for a built in type unless you attempt to take its address (a pointer to a const) in which case it will instantiate it with static linkage, you may then also get multiple instantiations in separate modules, but unless you expect all pointers to the same const to have the same address, this is not a problem.

Solution 6 - C++

You need to declare the contants in the header and then define them in one of your code files. If you do not declare them anywhere, then there is a linker error when it tries to tie the declaration to the actual definition. You can also get away with using #ifdef statements to have one definition within the header.

Make sure they are declared in a header that is included by everyone that needs them and make sure they are defined exactly once.

Jacob

Solution 7 - C++

If you want to define constants in header files, use static const. If you use extern, the linker is right to complain about multiple definitions because each including source file will supply memory for the variable if you assign a value.

Solution 8 - C++

It looks like that header file is getting included multiple times. You need to add guards.

At the top of each header file you should have something like:

#ifndef MY_HEADER_FILE_NAME_H
#define MY_HEADER_FILE_NAME_H

...

// at end of file
#endif

If you are using g++ or MSVC then you can just add:

#pragma once

At the top of each header file, but that isn't 100% portable.

Also, you shouldn't define constants in header files, only declare them:

// In header file
extern const int my_const;


// In one source file
const int my_const = 123;

Solution 9 - C++

in declaring global const within header causes that each compilation unit including this hader will have own definitions global definitions with the same name. Then linker does not like that.

If You really need these in header then probably You should declare them as static.

Solution 10 - C++

An old question, indeed, but one useful answer is missing.

It is possible to trick MSVC into accepting static constants in headers simply by wrapping those in a "dummy" class template:

template <typename Dummy = int>
struct C {
     static const double Pi;
};

template <typename Dummy = int>
const double C<Dummy>::Pi = 3.14159;

Now, C<>::PI can be accessed from elsewhere. No redefinition complains; constant is directly accessible in each compilation units without fancy link time optimization. Macro can be rolled out to further prettify this approach (even though macros are evil).

Solution 11 - C++

I too ran into this issue:

static const uint64 GameTexSignature = 0x0a1a0a0d58455489;

would not compile on Linux when defined in a header file. It compiles fine with MSVC. The fix that worked for me is changing it to:

static constexpr uint64 GameTexSignature = 0x0a1a0a0d58455489;

This was only required for the uint64 constant, not any of the uint32 constants. I think the following explains it, but that does imply the Linux compiler doesn't consider a const uint64 an integral constant.

https://exceptionshub.com/how-to-declare-a-static-const-char-in-your-header-file.html

Cheers John

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnumerical25View Question on Stackoverflow
Solution 1 - C++AnTView Answer on Stackoverflow
Solution 2 - C++Carl NorumView Answer on Stackoverflow
Solution 3 - C++Jerry CoffinView Answer on Stackoverflow
Solution 4 - C++John BodeView Answer on Stackoverflow
Solution 5 - C++CliffordView Answer on Stackoverflow
Solution 6 - C++TheJacobTaylorView Answer on Stackoverflow
Solution 7 - C++ChristophView Answer on Stackoverflow
Solution 8 - C++Peter AlexanderView Answer on Stackoverflow
Solution 9 - C++lollinusView Answer on Stackoverflow
Solution 10 - C++oakadView Answer on Stackoverflow
Solution 11 - C++John ScottView Answer on Stackoverflow