Setting /MT flag when compiling C & C++ drivers for Windows

Hi,

I am having trouble compiling both the MongoDB C driver (v1.29.2) and the C++ driver (v3.11.0) using CMake. All of our projects which use the drivers (“-static” libs) are statically linked against the CRT in the Release configuration, so we need the static libs to also be statically linked against the CRT.

However, setting the relevant flags (e.g. MSVC_RUNTIME_LIBRARY, CMAKE_CXX_FLAGS_RELEASE, etc.) does not seem to have the intended effect. The output is always defined to be dynamically linked to the CRT no matter what. In order to change this, we have to either manually set the flag in the VS projects between the configuration and build steps or edit the CMakeLists.txt to set the MSVC_RUNTIME_LIBRARY for the relevant targets (i.e. all four static libs and utf8proc_obj).

I reckon this is not the intended behaviour, and having to manually edit the cmake files every time we want to update the libraries is inconvenient at best. Am I doing something wrong?

Below are the commands I am using to compile the drivers. As you can see, I have tried setting quite a few different options, but none of them have the intended effect:

cd %MONGO_C_DRIVER_PATH%
cmake .^
 -B./lib64MT^
 -A x64^
 -DCMAKE_BUILD_TYPE=Release^
 -DBUILD_VERSION=1.29.2^
 -DENABLE_MONGODB_AWS_AUTH=OFF^
 -DCMAKE_CXX_FLAGS_RELEASE="/MT"^
 -DCMAKE_C_FLAGS_RELEASE="/MT"^
 -DBUILD_TESTS=OFF^
 -DCMAKE_INSTALL_PREFIX=%MONGO_x64_C_INSTALL_PREFIX%

cmake --build ./lib64MT --config Release --parallel --target clean
cmake --install ./lib64MT --config Release

cd %MONGO_CXX_DRIVER_PATH%
cmake .^
 -B.\lib64MT^
 -A x64^
 -DCMAKE_GENERATOR_PLATFORM=x64^
 -DBUILD_VERSION=3.11.0^
 -DCMAKE_BUILD_TYPE=Release^
 -DCMAKE_CXX_STANDARD=17^
 -DBSONCXX_POLY_USE_STD=ON^
 -DMSVC_RUNTIME_LIBRARY="MultiThreaded"^
 -DCMAKE_CXX_FLAGS_RELEASE="/MT"^
 -DCMAKE_C_FLAGS_RELEASE="/MT"^
 -DCMAKE_STATIC_LINKER_FLAGS_RELEASE="/MT"^
 -DCMAKE_PREFIX_PATH=%MONGO_x64_C_INSTALL_PREFIX%^
 -DCMAKE_INSTALL_PREFIX=%MONGO_x64_CXX_INSTALL_PREFIX%^
 -DBUILD_TESTS=OFF^
 -DENABLE_TESTS=OFF^
 -DBUILD_SHARED_LIBS=OFF^
 -DBUILD_SHARED_AND_STATIC_LIBS=OFF^
 -DENABLE_ABI_TAG_IN_LIBRARY_FILENAMES=OFF

cmake --build ./lib64MT --config Release --parallel --target clean
cmake --install ./lib64MT --config Release

Per the documentation, please try setting the DBUILD_SHARED_AND_STATIC_LIBS flag to on to build mongocxx as static library.

Enabling that flag gives me the following error:

CMake Error at CMakeLists.txt:175 (message):
  BUILD_SHARED_LIBS is OFF but BUILD_SHARED_AND_STATIC_LIBS is ON.  To build
  static libraries only, set both BUILD_SHARED_LIBS and
  BUILD_SHARED_AND_STATIC_LIBS to OFF

This message was actually why I ended up setting that flag to “OFF”.

How are you setting the MSVC_RUNTIME_LIBRARY?
You should be setting CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded (/MT) during CMake configuration. Editing the CMake config directly isn’t required.

$ cmake -S . -B build -D CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded -D BUILD_SHARED_LIBS=OFF