通过 CMake 连接 google 测试时,会创建一个模板文件 hello_test.cc。它不能重命名,其扩展名也不能更改为 .cpp。您也不能将其他 google-test 文件放在此目录中,因为找不到 #include <gtest.h>。
我在 Visual Studio 19 中执行了所有相同的操作,在那里一切正常,添加了更改。
问题是如何解决这个问题?
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(google)
set(CMAKE_CXX_STANDARD 17)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
set(BUILD_SHARED_LIBS OFF)
add_executable(google src/main.cpp)
file(GLOB tests
"${PROJECT_SOURCE_DIR}/google_tests/*.cpp"
"${PROJECT_SOURCE_DIR}/google_tests/*.c"
"${PROJECT_SOURCE_DIR}/google_tests/*.cc"
)
add_executable(
google_test
${tests}
)
target_link_libraries(
google_test
gtest_main
)
include(GoogleTest)
gtest_discover_tests(google_test)
每次编辑时,每次都需要重新加载 CMakeLists.txt,然后一切正常。一般来说,我是krivorukov rukozhop。实际上,那里没有创建任何文件。