Conflicting run time libraries in Visual C++ DLL project

故事扮演 提交于 2019-12-25 05:06:23

问题


I Just tried to build a x64 C++ DLL using Visual C++ 2012. It's a simple DLL linking one other static third-party .lib file. I am getting the following warning message:

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

It seems that other library uses /MT (multithreaded static runtime) linker option while my DLL (and all the other projects importing my DLL) uses /MD (multithreaded DLL runtime). I can not harmonize this since the third-party lib is delivered as it is and changing my DLL would create the same problem in all projects using my DLL.

I read some pages from MS and forums about this problem. But non of these pages explained what exactly is the problem here.

What I do not understand:

What is the harm in linking code that uses different runtime variant (other than wasting memory)? Some people say this can be ignored, some say it may not.

Is it ok to use /NODEFAULTLIB[:library]? The documentation says it would "remove a specified library or libraries from the list of libraries it searches when resolving external references". Which library should I add as ":library" and in which way does this solve the problem?


回答1:


Conflicting run-time libraries can cause serious problems if you are sharing C run time objects (items such as FILE*) or sharing memory allocations (allocating memory in one part and deallocating it in another). I would avoid "forcing" them together to avoid weird problems and crashes.

Instead, if you cannot change the build of the lib or of your dll, I would wrap the lib inside a dll with a C only interface that doesn't leak any C runtime object or require you to free memory outside of the dll that was created inside.




回答2:


For example passing STL types (e.g. std::string) could lead to major crashes due to differnce in the internal object layout and/or memory disposal

See Why does this program crash: passing of std::string between DLLs



来源:https://stackoverflow.com/questions/19712236/conflicting-run-time-libraries-in-visual-c-dll-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!