cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Document how to get a MSVCRTD-based debug-build LIB on Windows

Open uazu opened this issue 3 years ago • 3 comments

This took us a while to figure out. A Rust "staticlib" debug-build outputs a LIB which references MSVCRT. This is not suitable for linking with C++ code built using MSVC debug settings (/MDd), which requires everything to use only MSVCRTD. To get a Rust LIB which references MSVCRTD appears to require CFLAGS=-MDd and CXXFLAGS=-MDd set in the Windows environment (e.g. set CFLAGS=-MDd etc at CMD prompt). This works because cc crate picks these up when it calls cl.exe. In our use-case this caused all the mentions of MSVCRT in the dumpbin /all output for the generated LIB to be replaced with MSVCRTD. Then the link of the LIB to debug C++ code succeeds.

So the question is where this should be documented. It is no use documenting this in cc crate, because that is deep down the crate tree and no-one is going to look there. Perhaps people don't even realize that cc crate is being used. Since it's often someone directly using cxx crate to interface to C++ who would hit this problem, it would likely be helpful to document it in the cxx.rs pages.

(Thanks for cxx crate, by the way -- it helped us a lot!)

uazu avatar Jun 03 '21 17:06 uazu

Ran into the same issue (windows, clang-cl), in debug mode you either have to compile the c++ part with /MT or the rust part with /MDd, otherwise they arent compatible for linking, this should honestly be documented somewhere

razaqq avatar Aug 12 '22 13:08 razaqq

@uazu Were you able to get a /Mtd build by setting the CFLAGS ? It might be simple but how to set such CFLAGS in cargo?

Jashwanth537 avatar Jul 11 '23 06:07 Jashwanth537

@uazu Were you able to get a /Mtd build by setting the CFLAGS ? It might be simple but how to set such CFLAGS in cargo?

Yes, we're still successfully building that way. Since this is a C++ project that calls Rust, we're setting CFLAGS in CMake, since that's the top-level tool that is building both the C++ code and the Rust code. (Don't ask me about CMake though.)

uazu avatar Jul 11 '23 14:07 uazu