【问题标题】:Error while integrating google test into existing CMake project将谷歌测试集成到现有 CMake 项目时出错
【发布时间】: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 编辑为testinclude(${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


【解决方案1】:

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

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

根据include 的文档,此命令将:

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

此命令通常保留用于加载 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 文件中的其他相对引用进行类似的更改,以防止这种异常方法导致错误。


我的建议是从您的 top-level CMakeLists.txt 文件中运行 CMake,然后只需使用以下行遍历子目录中的 CMake 文件:

add_subdirectory(unitTest)

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 1970-01-01
    • 2018-03-09
    • 2018-01-09
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多