Visual C++ Runtime Library Linker Woes

2019-07-14 04:20发布

问题:

Observe this close Scenario even though it appears to be the same as my previous questions. Still I am not getting an answer. So please don't report as a duplicate.

I have a project which has 10 dependencies. First I compiled using the /MTD option in the C/C++ codegeneration section in the main project and all its dependencies are getting build successfully.

Next I changed the option from /MTD to /MDd and again all dependent projects are getting build successfully. But for the main project the following errors are reported:


LIBCMTD.lib(osfinfo.obj) : error LNK2005: __open_osfhandle already defined in MSVCRTD.lib(MSVCR80D.dll)

LIBCMTD.lib(lseeki64.obj) : error LNK2005: __lseeki64 already defined in MSVCRTD.lib(MSVCR80D.dll)


sqlite3x.lib(sqlite3x_command.obj) : error LNK2005: "protected: wchar_t * __thiscall std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >::_Myptr(void)" (?_Myptr@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@IAEPA_WXZ) already defined in msvcprtd.lib(MSVCP80D.dll)

MSVCRTD.lib(MSVCR80D.dll) : error LNK2005: __mkdir already defined in LIBCMTD.lib(mkdir.obj)

MSVCRTD.lib(MSVCR80D.dll) : error LNK2005: __strdup already defined in
LIBCMTD.lib(strdup.obj)

   Creating library Debug/Application.lib and object Debug/Application.exp
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library

LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library

How can I fix this?

回答1:

You cannot mix C runtime libraries. If you have a library or object compiled /MT(anything), you cannot just link with /MD. You need to link with the threadsafe MSVCRT. There's no if's and's or but's about it. You CANNOT mix C runtimes. I've always found it best, even in programs that don't do threading, to just use /MT.

Did you run a project clean operation to remove already built objects and libraries? You have a dependency on SQLite here as well, did you rebuild that, too?



回答2:

When a bunch of static c++ library are linked all together , they all must have /MTD or they all must have /MDd.You can't link a project with /MTD with another project with /MDd

This is probably the reason for your linking errors. The reason why you are getting it only on your main project is that your main project is the only one that actually do the linking.Please tell us if it resolved your problem.

I recall I once had the same problem with /MTD and /MDd and I had very similar errors.



回答3:

My answer here might be of some help, linking to some MSDN articles I found very useful in solving my version of this problem.