【发布时间】:2020-11-11 01:32:30
【问题描述】:
我有一个工作项目,我想在其中添加 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 也需要的库和文件,但它也会创建一个可执行文件。我尝试使用这个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)
【问题讨论】:
-
您还没有显示所有代码。错误发生在这里
test/CMakeLists.txt,但您没有在项目层次结构中提到此 CMake 文件,也没有显示将main.cpp添加到目标的 CMake 代码。这里没有足够的信息来了解发生了什么,所以请创建一个Minimal, Reproducible example 来演示这个问题。此外,这条线看起来很奇怪include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt),应该避免使用。 -
@squareskittles 将
root编辑为test,include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt)行也用于链接所有库和项目文件 - 有没有办法在不将所有test/CMakeList.txt复制到`test/unitTest/CMakeList.txt? -
通常,通过在项目层次结构中的顶级 CMake 文件上发出
cmake命令来运行CMake。如果这样做,您可以使用顶级 CMake 文件中的命令add_subdirectory()遍历到unitTest子目录中的 CMake 文件。不建议像您的示例那样向后向上遍历父目录。无论如何,添加main.cpp的CMake 代码仍然没有显示。 -
@squareskittles 添加了
test/CMakeList.txt
标签: c++ cmake googletest