【问题标题】:Adding google-test to a subfolder in a CMake project将 google-test 添加到 CMake 项目中的子文件夹
【发布时间】:2013-08-04 14:18:27
【问题描述】:

我刚刚在我的项目中将 google-test 源代码添加到 libs/gtest-1.6.4 目录。有一个libs/gtest-1.6.4/CMakeLists.txt 文件。在最上面的CMakeLists.txt 中,我添加了add_subdirectory('libs/gtest-1.6.4')。项目的结构是

|- CMakeLists.txt 
|- src 
   |- CMakeLists.txt 
   |- *.h and *.cc 
|- libs
   |- gtest-1.6.4
      |- CMakeLists.txt
      |- gtest source code etc.
|- other subdirectories 

现在我在其中一个头文件中添加#include "gtest/gtest.h"。编译失败并出现

gtest/gtest.h: No such file or directory
compilation terminated.

这是我的src/CMakeLists.txt 文件的 sn-p。

set( Boost_USE_STATIC_LIBS ON )
find_package( Boost COMPONENTS graph regex system filesystem thread REQUIRED)

.. Normal cmake stuff ...
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} )

# This line is added for google-test
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS} ${COMMON_INCLUDES})

add_executable(Partitioner
  print_function.cc
  methods.cc
  partitioner.cc
  main.cc
  )

TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${GTEST_LIBRARIES})

我错过了什么?

【问题讨论】:

  • 顺便说一句(因为它没有回答您的问题),您是否考虑过将 GTest 源代码添加到您的源代码树中?您可以使用 CMake 的 ExternalProject 模块下载 GTest 并将其构建到您的构建树中,而不是将 3rd 方代码添加到您自己的源代码树中。我有一个答案 here 显示如何做到这一点。
  • 是的。但是我们使用私有 git 存储库。我们尝试将大部分库保留在项目中,以便我们以后不必担心版本不兼容。
  • 来晚了,但只是想说我们使用 git 子模块进行 gtest 效果很好。

标签: cmake googletest


【解决方案1】:

查看GTest's CMakeLists.txt,看起来它们的包含路径是${gtest_SOURCE_DIR}/include。他们还将库定义为名为 gtest 的 CMake 目标(这包含在当前位于第 70 行的宏 cxx_library(gtest ...) 中)。

所以看起来你需要这样做:

...
# This line is added for google-test
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS} ${COMMON_INCLUDES})
INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${COMMON_INCLUDES})
...
TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${GTEST_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES} gtest)

您还必须确保在您的根 CMakeLists.txt 中,您已调用 add_subdirectory(libs/gtest-1.6.4) 之前 add_subdirectory(src) 以便在使用 GTest 变量时正确设置它们在“src/CMakeLists.txt”中。

【讨论】:

  • 我认为您真的想使用:target_include_directories 到 google 测试目标,以便从 src 目录中找到您的头文件。像这样:target_include_directories(thisismy_test PRIVATE ${CMAKE_SOURCE_DIR}/src)
【解决方案2】:

accepted answer中所述,

我确实把我的 GoogleTest 东西放在了我的 add_subdirectory() 行之前,它起作用了。

# The ROOT CMakeLists.txt

enable_testing()

include(CTest)

# https://google.github.io/googletest/quickstart-cmake.html
include(FetchContent)
FetchContent_Declare(
        googletest
        URL https://github.com/google/googletest/archive/....zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)


add_subdirectory(some)
add_subdirectory(other)
add_subdirectory(another)

在其中一个子目录中,我看到了测试工作。

# A CMakeLists.txt in a sub directory

#enable_testing()    # NOT REQUIRED, hence, commented out
#include(CTest)      # NOT REQUIRED, hence, commented out
#include(GoogleTest) # NOT REQUIRED, hence, commented out

add_executable(
        mytest
        test/mytest.cpp
)
target_link_libraries(
        mytest
        gtest_main
)

gtest_discover_tests(
        mytest
)

【讨论】:

    猜你喜欢
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2016-01-30
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多