尝试获取 GoogleTest for C++ 项目时出现 CMake 构建错误

问题描述 投票:0回答:1

作为一个个人项目,我一直在尝试创建自己的 C++ 库,并且一直使用 Google 的 GoogleTest 进行单元测试。为了让您更好地了解我的项目的结构,这里是我的文件结构:

MyLibrary/
│
├── googletest/ (Literally the cloned github repo)
│
├── src/
│   └── ALL MY SRC FILES
│
├── tests/
│   ├── CMakeLists.txt
│   └── tests.cpp
│
├── CMakeLists.txt (What I call the "main" CMakeList)
└── README.md

我对这个问题进行了简化。

我之前执行此操作的方法是将整个 GoogleTest 存储库包含到我的项目中,并将该目录包含在我的主 CMakeList.txt 中,如下所示:

cmake_minimum_required(VERSION 3.27.2)

set (This MyLibrary)

project (${This} C CXX)

set (CMAKE_C_STANDARD 99)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_POSITION_INDEPENDENT_CODE ON)

enable_testing ()

add_subdirectory (googletest)

set (Headers
    #All *.hfiles
)

set (Sources
    #All *.cpp files
    #DOESNT INCLUDE THE tests.cpp
)

add_library (${This} STATIC ${Sources} ${Headers})

add_subdirectory(tests)

以及测试目录中的另一个 CMakeList.txt:

cmake_minimum_required(VERSION 3.27.2)

set (This tests)

set (Sources
    tests.cpp
)

add_executable (${This} ${Sources})
target_link_libraries (${This} PRIVATE
    gtest_main
    MyLibrary
)

add_test (
    NAME ${This}
    COMMAND ${This}
)

这在当时是有效的,但当我开始更多地考虑实际使用该库的其他人时,这根本不是一个好的解决方案。因此,我尝试在项目构建时使用 CMake 中的 FetchContent 下载存储库。这是我到目前为止在“主”CMakeList.txt 中得到的内容:

cmake_minimum_required(VERSION 3.27.2)

set (This MyLibrary)

project (${This} C CXX)

set (CMAKE_C_STANDARD 99)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_POSITION_INDEPENDENT_CODE ON)

enable_testing ()

include(FetchContent)
FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

set (Headers
    #All *.hfiles
)

set (Sources
    #All *.cpp files
    #DOESNT INCLUDE THE tests.cpp
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

add_library (${This} STATIC ${Sources} ${Headers})
target_link_libraries(${This} googletest)

add_subdirectory(tests)

这是测试目录中的 CMakeList.txt:

cmake_minimum_required(VERSION 3.27.2)

set (This tests)

set (Sources
    tests.cpp
)

add_executable (${This} ${Sources})
target_link_libraries (${This} PRIVATE
    GTest::gtest_main
    MyLibrary
)

add_test (
    NAME ${This}
    COMMAND ${This}
)

include(GoogleTest)
gtest_discover_tests(tests)

但是当我尝试构建我的项目时,我收到以下错误:

[main] Building folder: MyLibrary 
[build] Starting build
[proc] Executing command: chcp
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build "c:/Projects/MyLibrary /build" --config Debug --target all -j 14 --
[build] [ 37%] Built target MyLibrary
[build] [ 50%] Built target gtest
[build] [ 62%] Built target gtest_main
[build] [ 75%] Built target gmock
[build] [ 81%] Linking CXX executable tests.exe
[build] [ 93%] Built target gmock_main
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgoogletest: No such file or directory
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [tests\CMakeFiles\tests.dir\build.make:102: tests/tests.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:286: tests/CMakeFiles/tests.dir/all] Error 2
[build] mingw32-make: *** [Makefile:145: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build "c:/Projects/MyLibrary/build" --config Debug --target all -j 14 -- exited with code: 2
[driver] Build completed: 00:00:01.041
[build] Build finished with exit code 2

我希望我的项目能够在没有任何错误的情况下构建,然后允许我运行 ctest 在

tests/tests.cpp
中运行我的单元测试,但是当我在出现构建错误后尝试运行该项目时,我得到以下输出:

[main] =======================================================
[main] No executable target was found to launch. Please check:
[main]  - Have you called add_executable() in your CMake project?
[main]  - Have you executed a successful CMake configure?
[main] No program will be executed

所以我的测试根本不再起作用。

我尝试进行备份、删除构建并进行干净的构建,但这没有帮助。我对使用 CMake 还很陌生,所以我一直在尝试遵循在线指南。主要是这里的快速入门:https://google.github.io/googletest/quickstart-cmake.html。我是不是错过了什么?

c++ cmake googletest cmakelists-options
1个回答
0
投票
target_link_libraries(${This} googletest)

那条线是错误的。删除它。该错误是关于您尝试在上面设置的不存在的库

googletest
。您已正确设置
GTest::gtest_main
,就足够了。

© www.soinside.com 2019 - 2024. All rights reserved.