C++ 使用 Googletest cmake 设定

发布时间 2023-08-28 17:16:00作者: waters_yuan

 

项目 CMakeLists 文件中加入:
1 include(FetchContent)
2 FetchContent_Declare(
3         googletest
4         URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
5 )
6 
7 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
8 FetchContent_MakeAvailable(googletest)
View Code
 
在测试的 CMakeLists 文件中加入:
 1 enable_testing()
 2 
 3 add_executable(
 4         hello_test
 5         test1.cpp
 6 )
 7 target_link_libraries(
 8         hello_test
 9         GTest::gtest_main
10 )
11 
12 include(GoogleTest)
13 gtest_discover_tests(hello_test)
View Code
参考:https://google.github.io/googletest/quickstart-cmake.html