Mix code compiled with / MT and / MD

I have a big piece of code compiled with / MT (i.e. waiting for static binding to CRT). I need to combine this with a static third-party library that was built using / MD (i.e. For dynamic CRT binding).

Is it theoretically possible to link these two into one executable file without recompiling?

If I contact / nodefaultlib: msvcrt, I get a small number of undefined references to things like __imp__wgetenv . I am tempted to try to implement these functions in my own code, forwarding to wgetenv , etc. Should I try, or will I work right on the next issue?

Unfortunately, I am forbidden to prohibit the simple option of packing third-party code into a separate DLL: - /

+6
visual-c ++ build msvcrt
source share
3 answers

No. / MT and / MD are mutually exclusive.

All modules passed to this linker call must be compiled with the same runtime compiler option ( / MD , / MT , / LD ).

A source

+3
source share

I found such a solution in OpenSSL sources: all library obj files were compiled using the combination: /MT /Zl . As described by the author, this combination allows you to create a static library with the ability to compile with applications either dynamic CRT ( /MD ) or static CRT ( /MT ).

0
source share

I faced a similar situation when I had two libraries, one of which was built with MT, and the other with MD. I had to create an executable file that uses the functionality of both libraries. The library built as MD was a third party, so I could not rebuild it, and the library built as MT has many dependencies and built all of them, since MD is a big pain. I was getting an error from a third-party configuration header file, which made it mandatory to create an executable file as MD. I was looking for an easy way to pack third-party dlls as a separate dll, as mentioned in the question. However, I could not find a sufficient explanation online on this simple path. Hence my two cents below. The next one is how I get around it

  • I built another .dll that acted as an interface. This interface basically wrapped up all api calls that were made to a third-party dll. The header file for this interface did not contain a header file from a third-party DLL, and all these header files were included in the interface.cpp file. The interface, as you expected, was built as MD.
  • Now, in my main.cpp file, I have included this interface header file to make all calls to a third-party DLL through the interface.

  • Special care should be taken when passing arguments to an interface. Base variables, such as int, bool, etc., can be passed as a value. However, any class or structure must be passed as constant references to avoid damage to the heap. This applies to an even line.

Happy to share more details if this is not clear!

0
source share

All Articles