c++cmakegoogletestcmakelists-options

CMake build errors when trying to fetch GoogleTest for C++ project


As a personal project I have been trying to create my own C++ library, and I have been using Googles GoogleTest for unit tests. To give you a good idea of how my project is structured here is my file structure:

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

I simplified it down for this question ofc.

The way I did this before was by literally including the entire GoogleTest repo into my project, and including that directory inside my main CMakeList.txt as below:

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)

Along with another CMakeList.txt inside the tests directory:

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}
)

This worked for the time, but as I start to think more about other people actually using the library, this isnt a good solution at all. So instead I have tried to use FetchContent in CMake to download the repo when the project is built. This is what I have gotten so far in the "main" 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)

and here is the CMakeList.txt inside the tests directory:

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)

But when I try to build my project, I get these errors:

[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

I expected for my project to be able to build without any errors, then allow my to run ctest to run my unit tests in tests/tests.cpp, but when I try to run that after getting the build errors I get this output:

[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

So my tests are no longer working at all.

I tried making a backup, deleting the build and doing a clean build, but that didn't help. I am pretty new to using CMake, so I have been trying to follow online guides. Mainly the quickstart here:https://google.github.io/googletest/quickstart-cmake.html. Am I missing something?


Solution

  • target_link_libraries(${This} googletest)
    

    That line is wrong. Delete it. The error is saying about the non existing library googletest that you are trying to set above. You have set GTest::gtest_main up properly, it's enough.