Search code examples
c++cmakeclioncatch2

Catch2 CLion error, "No tests were found"


I have a folder structure like

enter image description here

and I am trying to get Catch2 setup, my CMake files look like:

the topmost CMake:

cmake_minimum_required(VERSION 3.21)
project(throwaway)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
add_subdirectory(tests)

add_executable(foo_main main.cpp)
target_link_libraries(foo_main PUBLIC foo_lib)

my src CMake:

add_subdirectory(foo)

my src/foo CMake:

add_library(foo_lib Foo.cpp)

my tests CMake:

add_subdirectory(foo)

add_executable(foo_test catch_runner.cpp)
target_link_libraries(foo_test PUBLIC
        foo_test_lib)

my tests/foo CMake

add_library(foo_test_lib
        FooTests.cpp)

target_link_libraries(foo_test_lib PUBLIC
        foo_lib)

From there, I used Clion's Catch2 integration to set up my run config as enter image description here

so nothing crazy here

However, I get this error: enter image description here

I discovered that the error goes away if I edit the tests CMake into

add_executable(foo_test catch_runner.cpp foo/FooTests.cpp)
target_link_libraries(foo_test PUBLIC foo_lib)

and the test works as expected. But I obviously don't want to manually add each file into an executable, I want to be able to make a library that I can just slap the catch_runner into.

I have no clue why the test doesn't work when I link it as a library, but works when I add it manually. Any ideas?


Solution

  • Figured out it from the links at How do you add separate test files with Catch2 and CMake?

    in my tests/foo CMake I changed it to

    add_library(foo_test_lib OBJECT
            FooTests.cpp)
    
    target_link_libraries(foo_test_lib PUBLIC
            foo_lib)