我刚刚在项目的libs/gtest-1.6.4
目录中添加了google-test源代码。有一个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
文件的摘要。
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})
我错过了什么?
答案 0 :(得分:5)
查看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”中使用。