1

Suppose I created a header file constants.h and this file contains:

extern const int YEAR = 2011; // definition

If I tried in a cpp file (MainCPP.cpp) to use this constant after declaring it without defining it and without including the constants.h file as following:

extern const int YEAR; // declaration

int main() {
    cout << YEAR << endl;
}

When I try to do that I get: unresolved external symbol "int const YEAR". On the other hand, if I placed the definition of YEAR within constant.cpp file and done the same in MainCpp.cpp, I will not get the error and the linker will be able to link with YEAR defined within constants.cpp (without including constants.cpp in MainCpp.cpp here too).

Does this mean that the linker can link with source file code but not with header file code unless you explicitly included the header file.

Nizar
  • 416
  • 3
  • 13
  • Thanks [Rob K](http://stackoverflow.com/users/53089/rob-k) .. I tried to include constants.h in another cpp file (not MainCpp.cpp) and now the linker was able to link YEAR used in MainCpp.cpp file. This proves that the compiler doesn't compile the .h file. It includes it into the .cpp file where it is #included by texutally replacing the #include <...> with the content of the .h file and that no object code is generated for the .h file itself. – Nizar Dec 02 '11 at 05:31

2 Answers2

3

The declaration in the header file should be a declaration, not a definition. In other words, don't put the "= 2011" in your header.

This variable needs to be defined somewhere, such as your main.cpp. Don't put the extern in that definition.

Finally, if you want to assign a value to the variable at runtime, you cannot declare it with the const qualifier.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • I want in my header file constants.h to put the constants that I will use in my application. The declaration and definition should be in the header file. My question is why if I didn't include the header file and used YEAR the linker cannot find it while if I placed the declaration and definition of YEAR within another cpp file and did not include it, the linker will be able to find YEAR? – Nizar Dec 01 '11 at 19:55
1

extern tells the compiler that space is allocated for it somewhere else. There must be a definition of it somewhere without an extern on it. BUT in C++ (unlike C), consts have internal linkage, so you don't need the extern on it. (see Why does const imply internal linkage in C++, when it doesn't in C?)

Just put const int YEAR = 2011; in your header file and include your header file where ever you need it.

Community
  • 1
  • 1
Rob K
  • 8,757
  • 2
  • 32
  • 36
  • I know that I can define YEAR within the header file and include it but my question is why if I did not include it the compiler cann't link with the definition in the header file? – Nizar Dec 01 '11 at 19:52
  • On the other hand, if I declared and defined YEAR in cpp file (constants.cpp) and used YEAR in main.cpp without including constants.cpp, the linker will be able to link with YEAR. – Nizar Dec 01 '11 at 19:57
  • The compiler doesn't compile the .h file. The compiler includes it into the .cpp file where it is #included by texutally replacing the `#include <...>` with the content of the .h file. No object code is generated for the .h file itself. Now, if you omit the extern from the definition in the .h file, the C++ compiler can use the literal value of the const directly in the code and likely not need to allocate any memory location for it. – Rob K Dec 01 '11 at 20:11