Home > Enterprise >  Catch2 compile error (no such file or directory)
Catch2 compile error (no such file or directory)

Time:11-26

I've already used Catch2 for testing sucessfully, but this time a problem occurred. I am pushing Catch2 submodule to me project (this is not a -v2.x branch) and include "../Catch2/src/catch2/catch_all.hpp" to my test files. The problem is that in catch_all.hpp all of the included .hpp files (like <catch2/benchmark/catch_benchmark_all.hpp>, <catch2/catch_approx.hpp> and so on) are not found. I've checked paths but they seem to be fine. Any ideas what is wrong with it?
Here's an example of my code.

I am adding a Catch2 submodule with command

git submodule add https://github.com/catchorg/Catch2.git

CMakeFile.txt:

cmake_minimum_required(VERSION 3.20)
project(proj)

set(CMAKE_CXX_STANDARD 17)

set (sources
        ./main.cpp
        ./test_main.cpp
        ./test.cpp)

add_executable(mainApp ${sources})
target_compile_options(mainApp PRIVATE -Wall -pedantic -std=c  17)
target_link_libraries(mainApp)

set (tests
        ./test_main.cpp
        ./test.cpp)

add_subdirectory(Catch2)
add_executable(runTests ${tests})
target_compile_options(mainApp PRIVATE -g)
target_link_libraries(runTests PRIVATE Catch2::Catch2WithMain)

test_main.cpp:

#define CATCH_CONFIG_MAIN
#include"Catch2/src/catch2/catch_all.hpp"

test.cpp:

#include"Catch2/src/catch2/catch_all.hpp"

TEST_CASE("BasicTest_1", "FirstTest") {
    REQUIRE(1 == 1);
}

main.cpp is just a simple helloworld for now.

TEST_CASE doesn't work either, it says "C requires a type specifier for all declarations".

CodePudding user response:

You are including test_main.cpp and test.cpp in mainApp.

Which means that files in mainApp try to #include"Catch2/src/catch2/catch_all.hpp" without linking to the Catch2 library and includes.

Remove the test files from the mainApp sources and try again.

CodePudding user response:

It looks a bit different on my side. I must admit that I don't know what precisely is wrong on OP's side. Nevertheless, I present mine as it's working:

Assume, there is a project for a library rfUtil in ./Util.

It contains a sub-folder tests, which is added using add_subdirectory(tests) in ./Util/CMakeLists.txt.

./Util/tests/CMakeLists.txt:

project(rfUtilTests)

find_package(Catch2 CONFIG REQUIRED)

file(GLOB headers *.h)
file(GLOB sources *.cc)

add_executable(rfUtilTests
  ${sources} ${headers})

set_target_properties(rfUtilTests
  PROPERTIES
    PROJECT_LABEL "Utilities - Tests")

target_link_libraries(rfUtilTests
  rfUtil Catch2::Catch2)

# add test as rfUtilTests:
add_test(NAME rfUtilTests
  COMMAND rfUtilTests)

(I removed some additional stuff to organize folders for Visual Studio to make it self-standing.)

The test sources look like this:

The main test source ./Util/tests/rfUtilTests.cc:

// Catch2 header:
#define CATCH_CONFIG_MAIN
#include <catch.hpp>

and test for a specific class ./Util/tests/rfLockBoolTest.cc:

// own header:
#include <Util/rfUtil.h>
#include <Util/rfLockBool.h>

// Catch2 header:
#include <catch.hpp>

namespace RF::LockBoolTest {

bool lockRecursive = false;
static uint n;

void recursive()
{
  if (lockRecursive) return;
  const LockBool lock(lockRecursive);
    n;
  recursive();
}

} // namespace RF::LockBoolTest

TEST_CASE("RF::LockBool")
{
  REQUIRE([&]() {
    using namespace RF::LockBoolTest;
    lockRecursive = false; n = 0;
    recursive();
    return !lockRecursive && n == 1;
  }());
}

The Catch2 is installed via vcpkg on my side. (In case, the CMAKE_PREFIX_PATH is adjusted to make vcpkg packages visible to CMake.)


I remember that I used an alternative workflow for my very first fiddling with Catch2 – according to a suggestion found in the doc. of Catch2: I just copied the stand-alone header into the source folder of my test source files. That was working as well.

However, recalling the current doc. of Catch2 that might've been changed since then. (It looks like there isn't a header-only version anymore.):

How to migrate projects from v2 to v3

To migrate to v3, there are two basic approaches to do so.

  1. Use catch_amalgamated.hpp and catch_amalgamated.cpp.
  2. Build Catch2 as a proper (static) library, and move to piecewise headers

Doing 1 means downloading the amalgamated header and the amalgamated sources from extras, dropping them into your test project, and rewriting your includes from <catch2/catch.hpp> to "catch_amalgamated.hpp" (or something similar, based on how you set up your paths).

  • Related