GoogleTest & Cmake 问题

GoogleTest & Cmake issue

我正在尝试将 gtest 用于我的一个基于 cmake 的项目。

这是他们在 github 上提供的 cmake,用于在配置时构建 gtest(删除注释以使其更短):

configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
                 ${CMAKE_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include")
endif()

add_executable(example example.cpp)
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)

现在,在一个非常简单的项目中使用它时:

.
├── CMakeLists.txt
├── CMakeLists.txt.in
├── build
└── gtest_example.cpp

它按预期工作。但是,当在第一个 CMakeLists.txt 中添加具有自己的 CMakeLists.txt 和 CMakeLists.txt.in 以及 add_subdirectory(test) 的测试目录时,它不再起作用。

似乎 execute_process() 失败了,因为输入了 if(result)

我做错了什么?

发现这个问题于 github which solved my problem. See file changes 了解详情。

如果有人遇到同样的问题,将接受此答案。