Search code examples
c++buildcmakeheader-filescatch-unit-test

CMake header only dependency


I am having some trouble running a simple test with catch2 using CMake. Since catch is header only I got the impression that it is easily dropped into any project so i thought I'd just include it like a normal header file.

Project structure:

-build
-external
    -Catch2
        catch2.hpp
 CMakeLists.txt
 tester.cpp

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(Test VERSION 1.0.0)

include_directories(external)

enable_testing()
add_executable(tester tester.cpp)
add_test(Tester tester)

tester.cpp:

#define CATCH_CONFIG_MAIN
#include "Catch2\catch.hpp"

TEST_CASE( "1 is 1" ) {
    REQUIRE( 1 == 1 );
}

Output:

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.03 sec

The following tests FAILED:
          1 - Tester (Exit code 0xc0000139
)
Errors while running CTest

Obviously the test should pass, but it does not. Since I am a beginner both in the realm of CMake and catch2 I am having a hard time to pinpoint the issue. What I can say for certain is that catch.hpp is found and there is no linker errors, it just return some error code.

I looked around and found this:

CMake Header from library not found when implementing in catch test case

But it has no answers, and the author does not seem to have the same problem anyway.

This is how I build and run the tests (standing in the build directory):

cmake .. -G "MinGW Makefiles" && mingw32-make && ctest

Any help is appretiated :)


Solution

  • Alright, i removed catch and just played around with mingw and apparently i get the same error just by using std::string. Someone said that it has to do with missing DLL files. I ran dependency walker on the executable and indeed a bunch of DLLs was missing. I didn't now what to do or where to get these so i ditched mingw and tried the cygwin approach.

    But using cmake with cygwin i did not find any compatable generators for my development environment (windows).

    I then switched to generating a visual studio project instead (which i was avoiding from the start because i did not want to develop in an IDE). But i found out that i can use msbuild to build the executable from the generated visual studio project and it works like a charm, with catch.