gpt4 book ai didi

c++ - 将 google test 集成到现有 CMake 项目时出错

转载 作者:行者123 更新时间:2023-12-01 14:20:06 24 4
gpt4 key购买 nike

我有一个工作项目,我想将 google test 添加到其中,但有一个区别是,我的 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

我的 main CMakeLists.txt 文件链接了很多 test_main.cpp 也需要的库和文件,但它也会创建一个可执行文件。我尝试使用这个 question但我不知道如何让它工作,甚至不知道如何让它工作。

这是我的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 文件来自 google test git hub ,我也尝试做 Clion 手册 wrote ,但它仍然无法正常工作。这是我得到的错误:

-- 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 的文档,此命令将:

Load and run CMake code from a file or module.

此命令通常保留用于加载 CMake Find Modules或其他自定义 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)

关于c++ - 将 google test 集成到现有 CMake 项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63018420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com