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

百般思念 提交于 2019-12-17 20:22:22

问题


I am trying to integrate GTest with CMake as seamlessly as possible. But the default build type for my test projects are /MDd and GTest defaults to /MTd. I am manually changing GTest project properties to emit debug DLL.

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


回答1:


You can define gtest_force_shared_crt to ON before including gtest to achieve this. You can either do this via the command line:

cmake . -Dgtest_force_shared_crt=ON

or in your CMakeLists.txt:

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)



回答2:


I think a better option is @Fraser's answer - in that case, cmake + gtest 'just work'.

It's worth mentioning that in order to override the internal gtest option setting, you need to put the variable in the cmake cache:

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



回答3:


If Ted Middleton's answer doesn't work, try to use FORCE:

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

It worked for me




回答4:


We solved the problem by bypassing GTest's own build system and compiling GTest as a CMake object library from its unity build source file 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")

That way GTest will always be compiled with the same options that we use for the project. A test executable that uses GTest can then be built in the following way:

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


来源:https://stackoverflow.com/questions/12540970/how-to-make-gtest-build-mdd-instead-of-mtd-by-default-using-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!