Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ creates several symbols of a const?

Tags:

c++

gcc

In one of my header (C++) files I changed

   #define TIMEOUT 10

to the more(?) C++ way:

const int TIMEOUT = 10;

It seems however, g++(v 4.4.3) now includes this symbol several times in the binary.

$ nm -C build/ipd/ipd |head
08050420 T ToUnixTime
08050470 T ParseTime
080504c0 T ParseISOTime
080518e4 r TIMEOUT
080518ec r TIMEOUT
080518f4 r TIMEOUT
080518fc r TIMEOUT
080503e0 T HandleMessage

How come ?

like image 671
nissen Avatar asked Jan 23 '23 05:01

nissen


1 Answers

You have probably included your header in four separate translation units (.cpp files).

Namespace-scope const variables not declared extern are implicitly static, so there will be one for each translation unit in which the header is included.

like image 137
James McNellis Avatar answered Jan 24 '23 18:01

James McNellis