I’m working on a big c++ project and change code from other people. During this I got a linker error stating that reference VarA is multiple defined. I found the corresponding variable and it had been defined in a cpp file which had directly been included into the project. I tried to convert the source file in a header file and a source file, which doesn’t work.
I then tried to move the variable declaration into new separate h/cpp only containing this variable, which looks then like:
h-file (aaa.h)
#ifndef AAA_H
#define AAA_H
#include "classAdefinition.h"
extern ClassA VarA;
#endif
cpp-file (aaa.cpp)
#include "aaa.h"
ClassA VarA;
If I now include aaa.h in the main file, the linker error adds the new created aaa.obj to the error message (e.g. VarA is also defined there) which is exactly what I expected. But when I remove the definition of VarA in the main file I get a linker error staying that VarA is not defined, which is really confusing.
Does anyone have a clue what might be the cause of this behavior?
I’m using VS2008, and the project is created with cmake. Might this cause the problem? E.g. could there be a configuration issue? We also use templates quite often, can this lead to the problem?
I haven’t found the error, but the problem is now avoided because most of the third-party code has been changed and the error does no longer occure.