Hi, If I declare a global variable in a header file and include it in two .cpp files, the linker gives an error saying the symbol is multiply defined. My question is, why does this happen for only certain types of object (eg. int) and not others (eg. enum)?
The test code I used is given below:
test.h
#ifndef TEST_HEADER
#define TEST_HEADER
namespace test
{
int i_Test1 = -1;
int i_Test2 = -1;
};
#endif // TEST_HEADER
class1.h
#ifndef CLASS_1_HEADER
#define CLASS_1_HEADER
class class1
{
public:
void count();
};
#endif //CLASS_1_HEADER
class1.cpp
#include <iostream>
#include "class1.h"
#include "test.h"
void class1::count()
{
std::cout << test::i_Test1 << std::endl;
}
class2.h
#ifndef CLASS_2_HEADER
#define CLASS_2_HEADER
class class2
{
public:
void count();
};
#endif //CLASS_2_HEADER
class2.cpp
#include "class2.h"
#include <iostream>
#include "test.h"
void class2::count()
{
std::cout << test::i_Test2 << std::endl;
}
main.cpp
#include "class1.h"
#include "class2.h"
int main(int argc, char** argv)
{
class1 c1;
class2 c2;
c1.count();
c2.count();
return -1;
}
Building this code with:
g++ main.cpp class1.cpp class2.cpp -o a
produces the following output:
ld: fatal: symbol
test::i_Test1' is multiply-defined: (file /var/tmp//ccwWLyrM.o type=OBJT; file /var/tmp//ccOemftz.o type=OBJT); ld: fatal: symbol
test::i_Test2' is multiply-defined: (file /var/tmp//ccwWLyrM.o type=OBJT; file /var/tmp//ccOemftz.o type=OBJT); ld: fatal: File processing errors. No output written to a collect2: ld returned 1 exit status
If I change the test.h file as given below:
test.h (with enum)
#ifndef TEST_HEADER
#define TEST_HEADER
namespace test
{
enum val
{
i_Test1 = 5,
i_Test2
};
//int i_Test1 = -1;
//int i_Test2 = -1;
};
#endif // TEST_HEADER
I don't get the "multiply defined" error and the program gives the desired output:
5
6
-
That's because enumerations are not objects - they are types. Class types (class,struct,union) and enumerations can be defined multiple times throughout the program, provided all definitions satisfy some restrictions (summed up by the so-called One Definition Rule (ODR)). The two most important ones are
- All definitions have the same token sequence (textual identical)
- Names used must have the same meaning (resolve to the same things) in all definitions. (this is a requirement on the context of the definition)
Your enumeration definition satisfies all conditions of the ODR. Therefor, that is valid and no reason for the linker / compiler to moan (actually, for a violation of the ODR the compiler is not required to issue a message either - most of it falls under the so-called no diagnostic required rule, some violations also result in undefined behavior).
However, for every non-inline function and object, these must be only defined one time. Multiply defining those result in spurious errors, like in your case. To solve it, put only a declaration into the header file (using "extern" without an initializer) and put one definition into one of those .cpp files (omitting the "extern" then, or putting an initializer. If it is a const object, you still need the "extern", since per default const variables have internal linkage, and the symbol would not be exported otherwise).
0 comments:
Post a Comment