C++ – standard library already defined in lib, causing linker error

clinkerlnk2005

Not sure what I'm doing wrong here, but say I have:

foo.h

class foo
{
public:
int Get10(std::wstring);
};

foo.cpp

int foo::Get10(std::wstring dir)
{
   return 10;
};

And I compile it as a lib, if I include that lib in another project along with the relevant header (foo.h) and atttempt to call an instance of foo:

foo f;
f.Get10(L"ABC");

I get a linker error saying:

Error 1 error LNK2005: "public: __thiscall
std::_Container_base12::~_Container_base12(void)"
(??1_Container_base12@std@@QAE@XZ) already defined in
foo.lib(foo.obj) C:\foo\msvcprtd.lib(MSVCP100D.dll) footest

Any ideas why this happens?

Best Answer

Error 1 error LNK2005: "public: __thiscall std::_Container_base12::~_Container_base12(void)" (??1_Container_base12@std@@QAE@XZ) already defined in foo.lib(foo.obj) C:\foo\msvcprtd.lib(MSVCP100D.dll) footest

From what I can see, this error message means that you are trying to include MSVC runtime library twice. This could be due to the result of compiling the foo.lib with the Runtime library option: "Multi-threaded (/MT)" and the test project with the option: "Multi-threaded DLL (/MD)" for example.

Check the Runtime options under "Project Properties" ==> "C/C++" ==> "Code Generation" for both projects and make sure they are the same for both projects.

Related Topic