Standard library already defined in lib causing linker error

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

foo.h

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

foo.cpp

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

And I will compile it as lib if I include this lib in another project along with the corresponding header (foo.h) and try to call the foo instance:

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

I get a linker error:

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

Any ideas why this is happening?

+4
source share
2 answers

"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 see, this error message means that you turn on the MSVC environment library twice. This may be due to the result of compiling foo.lib using the Runtime library: Multi-threaded (/ MT) and a test project with the option: for example, a multi-threaded DLL (/ MD).

Check the execution parameters in Project Properties / C / C ++ / Code Generation for both projects and make sure they are the same for both projects.

+18
source

Do you include foo.h in any .h files? You may need to add header protectors to make sure that you do not define a class more than once for each 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

0
source

Source: https://habr.com/ru/post/1386215/


All Articles