6

I have a header file called abc.h in which i want to define a constant with an external linkage. Thus it contains the statement

---------------abc.h-------------------------

extern const int ONE = 1;

Next, i have main.cpp, where i want to use the value of ONE. Thus i declare the ONE in main.cpp before using it as

---------------main.cpp---------------------

extern const int ONE;
int main()
{
     cout << ONE << endl;
}

I get an error "Multiple definition of ONE".

My question is , how can i declare a const with external linkage, and use it subsequently in different files, such that there is ONLY one memory location for the constant as opposed to each file containing a static version of the constant.


I deleted the #include "abc.h" from main.cpp and everything works.

g++ abc.h main.cpp -o main

The address of ONE is same in header and the main. So it works.

But i dont understand how compiler resolves definition of ONE without include statement in the main.cpp

It seems like g++ does some magic. Is it a bad practice, where reader of main.cpp does not know where ONE is declared as there is no include "abc.h" in main.cpp ?

blue
  • 2,683
  • 19
  • 29
Jimm
  • 8,165
  • 16
  • 69
  • 118
  • Are you sure you have `extern` on both locations? (The code you have above compiled fine for me). Also, check out [Mixing extern and const](http://stackoverflow.com/questions/2190919/mixing-extern-and-const). – Jesse Good Mar 12 '12 at 03:10

2 Answers2

13

abc.h:

extern const int ONE;

abc.cpp:

#include "abc.h"

const int ONE = 1;

main.cpp:

#include "abc.h"

int main() {
     cout << ONE << endl;
}
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • interesting to note the extern declaration must be made in the same translation unit as the variable is defined. in this example you are effectively declaring it extern const first, then defining it in the same file. this is not required for non-const variables (just define them and they have external linkage) – tstittleburg Sep 04 '15 at 18:02
  • alternatively, `abc.cpp` can just contain a single line `extern const int ONE = 1;` For more explanation see C++ Primer 5th edition, p. 60. – Yibo Yang May 15 '17 at 02:11
  • 2
    @YiboYang How would main.cpp access ONE then? Also, please don't assume everybody has the same books as you do, and when you reference something try to make sure it can be accessed on-line and for free. – Branko Dimitrijevic May 15 '17 at 09:07
  • @BrankoDimitrijevic `ONE` is declared in `main.cpp` through the header `abc.h`. The book reference basically says in order to use a `const` variable cross multiple files, you can define it in one file (usually a source file), and declare it in other files that uses it (usually by means of a header file), and the definition/declaration should be `extern`. – Yibo Yang May 15 '17 at 15:00
  • @YiboYang You said: _"abc.cpp can just contain a single line `extern const int ONE = 1;`"_. I assumed you meant abc.h shouldn't contain anything. – Branko Dimitrijevic May 16 '17 at 06:58
2

The command line

g++ abc.h main.cpp -o main

causes the compiler to use header file abc.h as another module.

Usually one use #include "abc.h" and compiles with:

g++ main.cpp -o main


Line

extern const int ONE = 1;

is a definition, so it should be present in one module only. In headers we put declarations (without the assignments of actual value):

extern const int ONE;

They can be included into modules multiple times. When you include such declaration, your definition can omit "extern":

#include "abc.h"
const int ONE = 1;

(The question is 7 years old now, so the author know this already for sure, but I want to clarify it for others.)

rha
  • 577
  • 4
  • 4