将google测试集成到现有CMake项目时出错



我有一个正在工作的项目,我想在其中添加谷歌测试,但有一点不同,我的main()函数在不同的文件中。

这是我的项目结构:

-test
- include
- file.h
- file1.h
- file2.h
- file.cpp
- file1.cpp
- file2.cpp
- main.cpp
- CMakeLists.txt
- unitTest
-CMakeLists.txt
-test_main.cpp

我的mainCMakeLists.txt文件链接了许多库和test_main.cpp也需要的文件,但它也创建了一个可执行文件。我试着用这个问题,但我不知道如何让它发挥作用,甚至不知道它是否可能。

这是我的unitTest/CMakeList.txt:

cmake_minimum_required(VERSION 3.14)
# Download and unpack googletest at configure time
project(Google_tests)
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_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_CURRENT_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_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# Now simply link against gtest or gtest_main as needed. Eg
include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt)
add_executable(Google_tests test_main.cpp)
target_link_libraries(Google_tests gtest gtest_main)
add_test(NAME example_test COMMAND Google_tests)

这个CMakeList文件来自谷歌测试git hub,我也尝试过做Clion手册中写的,但它仍然不起作用。这是我得到的错误:

-- Configuring done 
CMake Error at /home/yaodav/Desktop/dnr_main_repo/test/CMakeLists.txt:74 (add_executable):   
Cannot find source file:
main.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm   .hpp .hxx .in .txx 
Call Stack (most recent call first):   
CMakeLists.txt:30 (include)
CMake Error at /home/yaodav/Desktop/dnr_main_repo/test/CMakeLists.txt:74 (add_executable):   
No SOURCES given to target: test 
Call Stack (most recent call first):   
CMakeLists.txt:30 (include)
CMake Generate step failed.  Build files cannot be regenerated correctly.

这是测试/CMakeList.txt:

cmake_minimum_required(VERSION 3.14)
project(test)
set(CMAKE_CXX_STANDARD 17)
if (CMAKE_BUILD_TYPE MATCHES Release)
add_definitions(-DRELEASE=1)
add_definitions(-DDEBUG=0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
else()
add_definitions(-DRELEASE=0)
add_definitions(-DDEBUG=1)
endif ()
include_directories(/include)

link_directories(list of dir)
#link_libraries(../lib/external/tinyxml2-master)
find_package(Threads) # for enableing std::thread
find_library(conn_LIB connlib)
SET(SOURCE_FILES main.cpp
file.cpp file.h
file1.cpp file1.h
file2.cpp file2.h )
add_executable(test ${SOURCE_FILES})
SET(LBS list of libs)
target_link_libraries(test ${list of libs} pq)

unitTest/CMakeLists.txt文件中的以下一行导致了您可能意想不到的行为:

include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt)

include的文档中,此命令将:

从文件或模块加载并运行CMake代码。

此命令通常保留用于加载CMake查找模块或其他自定义CMake模块(即*.cmake文件(。因此,此不会更改当前CMake源目录,因此test/CMakeLists.txt中提到的文件仍然被解释为相对于子目录test/unitTest。调用include()后,CMake在test/unitTest中查找main.cpp文件(以及其他文件(,但该文件不存在。因此,CMake打印错误。

虽然不推荐,但如果确实需要像这样遍历父CMakeLists.txt文件(使用include()(,则可以使用CMAKE_CURRENT_LIST_DIR引用源文件:

SET(SOURCE_FILES 
${CMAKE_CURRENT_LIST_DIR}/main.cpp
${CMAKE_CURRENT_LIST_DIR}/file.cpp
${CMAKE_CURRENT_LIST_DIR}/include/file.h
${CMAKE_CURRENT_LIST_DIR}/file1.cpp 
${CMAKE_CURRENT_LIST_DIR}/include/file1.h
${CMAKE_CURRENT_LIST_DIR}/file2.cpp 
${CMAKE_CURRENT_LIST_DIR}/include/file2.h 
)
add_executable(test ${SOURCE_FILES})

您可能需要对test/CMakeLists.txt文件中的其他相对引用进行类似的更改,以防止这种异常方法导致错误。


我的建议是从顶级CMakeLists.txt文件运行CMake,只需使用以下行即可遍历到子目录中的CMake文件:

add_subdirectory(unitTest)

最新更新