将 google 测试集成到现有 CMake 项目时出错
Error while integrating google test into existing CMake project
我有一个工作项目,我想向其中添加 google 测试,但有一点不同,我的 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
也需要的库和文件,但它也会创建一个可执行文件。我试过使用这个 但我不知道如何让它工作,即使它可能。
这是我的 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, and I also tried to do what the Clion Manual 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.
这里是 test/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 或其他 custom CMake 模块(即 *.cmake
文件)。因此,不会更改当前的 CMake 源目录,因此 test/CMakeLists.txt
中提到的文件仍然相对于 child目录test/unitTest
。调用 include()
后,CMake 在 test/unitTest
中查找 main.cpp
文件(和其他文件),但那里不存在。因此,CMake 打印错误。
虽然不推荐,但是如果你真的需要像这样遍历到parentCMakeLists.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
文件中的其他 相关 引用进行类似的更改,以防止这种异常方法导致的错误。
我的建议是从你的 top-level CMakeLists.txt 文件中 运行 CMake,然后简单地使用以下行遍历到sub-directory:
中的 CMake 文件
add_subdirectory(unitTest)
我有一个工作项目,我想向其中添加 google 测试,但有一点不同,我的 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
也需要的库和文件,但它也会创建一个可执行文件。我试过使用这个
这是我的 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, and I also tried to do what the Clion Manual 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.
这里是 test/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 或其他 custom CMake 模块(即 *.cmake
文件)。因此,不会更改当前的 CMake 源目录,因此 test/CMakeLists.txt
中提到的文件仍然相对于 child目录test/unitTest
。调用 include()
后,CMake 在 test/unitTest
中查找 main.cpp
文件(和其他文件),但那里不存在。因此,CMake 打印错误。
虽然不推荐,但是如果你真的需要像这样遍历到parentCMakeLists.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
文件中的其他 相关 引用进行类似的更改,以防止这种异常方法导致的错误。
我的建议是从你的 top-level CMakeLists.txt 文件中 运行 CMake,然后简单地使用以下行遍历到sub-directory:
中的 CMake 文件add_subdirectory(unitTest)