为什么Google测试会编译,但看不到我的项目源代码?
这是我得到的错误:
$ cmake . && make && make test
-- Configuring done
-- Generating done
-- Build files have been written to: mars_rover
Scanning dependencies of target mars_rover
[ 8%] Building CXX object CMakeFiles/mars_rover.dir/src/rover.cpp.o
Linking CXX executable build/bin/mars_rover
[ 33%] Built target mars_rover
[ 41%] Built target gtest
[ 50%] Built target gtest_main
Scanning dependencies of target mars_rover_TEST
[ 58%] Building CXX object CMakeFiles/mars_rover_TEST.dir/test/test_mars_rover.cpp.o
mars_rover/test/test_mars_rover.cpp:2:10: fatal error: 'src/directions.h' file not found
#include "src/directions.h"
^
1 error generated.
make[2]: *** [CMakeFiles/mars_rover_TEST.dir/test/test_mars_rover.cpp.o] Error 1
make[1]: *** [CMakeFiles/mars_rover_TEST.dir/all] Error 2
make: *** [all] Error 2
这是我安排项目的方式:
$ tree {src,test,lib} -L 1
src
├── directions.cpp
├── directions.h
├── main.cpp
├── plateau.cpp
├── plateau.h
├── point.h
├── rover.cpp
└── rover.h
test
├── input1
├── output1
├── test_main.cpp
└── test_mars_rover.cpp
lib
├── CMakeLists.txt
└── gtest-1.7.0
这是我的CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
# Options. Turn on with 'cmake -Dtest=ON'.
option(test "Build all tests." ON)
set(PROJECT_NAME mars_rover CXX)
project(${PROJECT_NAME})
set(CMAKE_CXX_FLAGS "-g -Wall -Werror")
file(GLOB_RECURSE PROJECT_SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/lib)
if (test)
add_subdirectory(lib/gtest-1.7.0)
enable_testing()
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
# finding test sources
file(GLOB_RECURSE TEST_SOURCES ${CMAKE_SOURCE_DIR}/test/*.cpp ${CMAKE_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME}_TEST ${TEST_SOURCES})
# link to gtest
target_link_libraries(${PROJECT_NAME}_TEST gtest gtest_main)
add_test(CardinalDirectionTests ${EXECUTABLE_OUTPUT_PATH}/${PROJECT_NAME}_TEST --gtest_filter=MarsRoverTest.CardinalDirectionTests)
add_test(RelativeDirectionTests ${EXECUTABLE_OUTPUT_PATH}/${PROJECT_NAME}_TEST --gtest_filter=MarsRoverTest.RelativeDirectionTests)
endif()
您的测试可执行文件找不到'src / directions.h',因为${CMAKE_SOURCE_DIR}
尚未将其父项()添加到include_directories
调用中。只需添加它就可以。
但是,我可以在您的设置中看到其他一些小问题。在您的CMakeLists.txt中运行,这是我要进行的更改:
cmake_minimum_required(VERSION 2.8.12)
如果可以将最小值指定为2.8.12,则可以访问更有用的target_include_directories
命令。哪里include_directories
有一些大手大脚的方法,但target_include_directories
您可以为该特定目标指定搜索路径。如果将其指定为PUBLIC
或INTERFACE
,则链接到该目标的后续目标也将自动继承这些搜索路径。
同样,您可以查看使用target_compile_definitions
和target_compile_options
。
set(PROJECT_NAME mars_rover CXX)
project(${PROJECT_NAME})
project(mars_rover)
该project
命令设置了变量PROJECT_NAME
,因此您的set
命令在这里几乎没有用。CMake将通过源文件后缀检测到这是一个C ++项目。
通过将所有源包含在两个单独的目标(主exe和测试exe)中,它们将全部编译两次。为了避免这种低效率,我会在“ / src”中创建一个除了“ main.cpp”之外的所有源代码的库,然后有一个仅包含main.cpp并指向该库的链接的exe 。
另外,不建议使用file(GLOB ...)
或file(GLOB_RECURSE ...)
作为收集源列表的一种方式。
set(CMAKE_CXX_FLAGS "-g -Wall -Werror")
file(GLOB_RECURSE PROJECT_SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
# No need to include .h files, but they show up in IDEs like Xcode if you do.
set(libSources
src/directions.cpp src/directions.h
src/plateau.cpp src/plateau.h
src/point.h
src/rover.cpp src/rover.h
)
add_library(${PROJECT_NAME}_LIB ${libSources})
# Anything linking to this will automatically have ${PROJECT_SOURCE_DIR}
# added to the compiler's search paths since it is PUBLIC. Prefer
# ${PROJECT_SOURCE_DIR} to ${CMAKE_SOURCE_DIR} in case this project is not
# the top-level one.
target_include_directories(${PROJECT_NAME}_LIB PUBLIC ${PROJECT_SOURCE_DIR})
# Use generator expressions to conditionally apply the flags to Unix and Debug
# builds. Anything linking to this will automatically have these flags also
# applied since they are PUBLIC.
target_compile_options(${PROJECT_NAME}_LIB PUBLIC
$<$<BOOL:${UNIX}>:-Werror -Wall $<$<CONFIG:Debug>:-g>>)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_LIB)
对于测试,您似乎有2个或3个main
函数-这将是一个问题:-)gtest_main
是一个小助手,它提供一个main
函数,该函数调用所需的gtest函数来调用测试。我猜您也已经main
在“ test / test_main.cpp”中,并且由于您当前包括了所有“ src /”文件,所以我想您也已经main
在“ src / main.cpp”中。
通过将mars_rover
目标分为lib和exe,您可以链接到lib并避免在测试中明确排除“ src / main.cpp”。我不能告诉您是要使用自己的“ test_main.cpp”还是要使用gtest的帮助程序-我假设后者是:
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
file(GLOB_RECURSE TEST_SOURCES ...)
add_executable(${PROJECT_NAME}_TEST ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}_TEST gtest gtest_main)
add_executable(${PROJECT_NAME}_TEST test/test_mars_rover.cpp)
# Only bring gtest's include path into scope - its src dir is not designed to
# be public
target_include_directories(${PROJECT_NAME}_TEST PRIVATE
${gtest_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME}_TEST ${PROJECT_NAME}_LIB gtest gtest_main)
最后,如果使用以下语法,则无需指定测试exe的完整路径:
add_test(NAME CardinalDirectionTests
COMMAND ${PROJECT_NAME}_TEST
--gtest_filter=MarsRoverTest.CardinalDirectionTests)
add_test(NAME RelativeDirectionTests
COMMAND ${PROJECT_NAME}_TEST
--gtest_filter=MarsRoverTest.RelativeDirectionTests)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句