10

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?

Helam
  • 1,385
  • 13
  • 17
meds
  • 21,699
  • 37
  • 163
  • 314

2 Answers2

28

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.

today
  • 32,602
  • 8
  • 95
  • 115
ksming
  • 1,422
  • 1
  • 16
  • 26
  • 3
    "with the Runtime library option: Multi-threaded (/MT) and the test project with the option: Multi-threaded DLL (/MD) for example." bingo, that was exactly it (came here to post that) – meds Dec 14 '11 at 03:07
  • I have experienced the same problem before so I knew what is going on when you posted that error :) – ksming Dec 14 '11 at 03:13
  • 1
    This other question is related: http://stackoverflow.com/questions/4917592/compiling-and-using-jsoncpp-on-visual-studio10-with-boost – John Dyer Jul 26 '13 at 15:27
  • 1
    Man sometimes C++ error messages blow my mind at how ambiguous they are. This is why I use C# for my main code base and C++ for components only. – zezba9000 Sep 06 '13 at 00:33
  • @ksming I have these settings and the error. But how do I change them in the Visual Studio GUI? – user1122069 Feb 13 '17 at 14:29
0

Are you including foo.h in any .h files? You may need to add header guards to make sure you do not define the class more than once per file:

#ifndef FOO_H_
#define FOO_H_

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

#endif  // FOO_H_

See also: http://en.wikipedia.org/wiki/Include_guard

sligocki
  • 6,246
  • 5
  • 38
  • 47