How to make GTest build / MDd (instead of / MTd) by default using CMake?

I am trying to integrate GTest with CMake as much as possible. But the default build type for my test projects is /MDd , and the default GTest is /MTd . I manually modify the properties of the GTest project to emit debug DLLs.

But every time I make changes to my CMakeLists.txt , GTest defaults to /MTd . How to stop it?

+8
visual-c ++ cmake googletest
source share
4 answers

We solved the problem bypassing our own build system GTest and compiling GTest as a library of CMake objects from the source file of the assembly source gtest-all.cc :

 # compile Google Test as an object library add_library(gtest OBJECT "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.6.0/src/gtest-all.cc") set_property(TARGET gtest PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.6.0" "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.6.0/include") 

Thus, GTest will always be compiled with the same parameters that we use for the project. Then the test executable that GTest uses can be built as follows:

 add_executable(test_executable ${TESTS_SRC} $<TARGET_OBJECTS:gtest>) add_test(NAME test COMMAND test_executable) 
0
source share

You can define gtest_force_shared_crt to ON before you enable gtest to achieve this. You can do this through the command line:

 cmake . -Dgtest_force_shared_crt=ON 

or in CMakeLists.txt :

 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 
+13
source share

I think the best option is @Fraser's answer - in this case cmake + gtest "just works."

It should be noted that to override the internal parameter gtest option you need to put the variable in the cmake cache:

 set( gtest_force_shared_crt ON CACHE BOOL "Always use msvcrt.dll" ) 
+13
source share

If the Ted Middleton answer doesn't work, try using FORCE:

 set( gtest_force_shared_crt ON CACHE BOOL "Always use msvcrt.dll" FORCE) 

It worked for me

+4
source share

All Articles