Pergunta

I have found other examples of people having this problem but have had no luck with their solutions. I am trying to use std::cout in a static library that also uses boost threads and bind. When I don't use and std::cout it compiles and links fine with the main program but when I do and I compile the library I have no problems but when I compile and link the main program that uses the static library I get a ton of things like:

2>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in MSVCRTD.lib(cinitexe.obj)
2>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in MSVCRTD.lib(cinitexe.obj)
2>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in MSVCRTD.lib(cinitexe.obj)
2>LIBCMT.lib(mlock.obj) : error LNK2005: __unlock already defined in MSVCRTD.lib(MSVCR100D.dll)
2>LIBCMT.lib(mlock.obj) : error LNK2005: __lock already defined in MSVCRTD.lib(MSVCR100D.dll)
2>LIBCMT.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR100D.dll)
2>LIBCMT.lib(crt0.obj) : error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj)

etc...

I have tried going in to my linker settings and stopping it from using the conflicting library listed in the error output, but I can't get it right. If I tell it to stop using one library it may fix the problems but give me a few missing external symbols, then I switch the library it was having problems with but I still get a few "already defined". Any clues to what I can do to fix this? Should I just make another class called "log" or something and just access it from the main .exe (which would use the strings library, which may cause problems again but I haven't tried it yet)? Thank you for any help.

Foi útil?

Solução

Try checking -> Project + Properties, C/C++, Code Generation, Runtime library. Your .lib and your main project must use the same setting here.

Outras dicas

Right-click your project, Properties, C/C++, Code Generation, Runtime Library setting. That's the source of your problem. Settings there are /MT and /MD. You are linking code that has conflicting values for this setting, everything must be compiled with the same one.

Boost could be the one. Check your linker's Additional Dependencies setting. IIRC, the .libs have mt or md in their name.

LNK2005 occurs when two compilation objects contain the same symbols. When you try to link them together, the linker doesn't know what to do with the fact it has duplicates of everything, hence the error.

Specifically, it sounds like your static lib / maybe boost may have been compiled with /MT which links the MSVC runtime in statically. It looks like you are trying to compile your code with the default options (/MD, a dynamic link to MSVCR100.DLL, MSVCP100.DLL).

See http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx

You can either rebuilt said static library ensuring it is linked against the dynamic libraries, or try your code linked statically.

I should add that the D on the end just means debug. i.e. MSVCP100D.DLL is the debug version of MSVCP100.DLL.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top