Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top