1

I'm trying to test my program by using Catch2 tests and build the application using CMake. The program works and the application gets build when the Catch2 tests are not implemented. Once I implemented the Catch2 tests, the application won't build anymore. I included #define CONFIG_CATCH_MAIN and #include "catch.hpp" into main.cpp.

But I always get something like this when I try to build the application with the tests included:

CMakeFiles\intent_recognition.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x43d): undefined reference to Catch::StringRef::StringRef(char const*)' CMakeFiles\intent_recognition.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x4b9): undefined reference to Catch::AssertionHandler::AssertionHandler(Catch::StringRef const&, Catch::SourceLineInfo const&, Catch::StringRef, Catch::ResultDisposition::Flags)'

I don't know what's going...

My main.cpp file looks like this:

#define CONFIG_CATCH_MAIN
#include "catch.hpp"

#include <iostream>
#include <string>
#include "Intent.h"

std::string func (std::string input)
{
    std::cout << input << std::endl;

    Intent IntentObj = Intent(input);       // create Intent Object with input

    IntentObj.analyze();

    return IntentObj.result();
}

TEST_CASE("Get Weather", "[func]")
{
    REQUIRE( func("What is the weather like today?") == "Intent: Get Weather" );
    REQUIRE( func("Tell me what the weather is like.") == "Intent: Get Weather") ;
    REQUIRE( func("I want to know the weather for tomorrow") == "Intent: Get Weather" );
    REQUIRE( func("Can you tell me the weather for Wednesday?") == "Intent: Get Weather") ;
} 

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.20.2)

project(intent_recognition)

add_executable(intent_recognition main.cpp)
fel1-xd
  • 21
  • 1
  • 3
  • how dd you use cach2 in `CMakeLists.txt`? it [works for me](https://github.com/MarekR22/class_of_in_cpp/blob/main/test/CMakeLists.txt). – Marek R May 03 '21 at 07:39
  • Well I don't know the test framework, but those errors seem to be linker errors. Do you have the library implementing those functions linked properly to the test executable? Please put your CMakeLists.txt into your question! – FloWil May 03 '21 at 07:45
  • So problem is invalid `CMakeLists.txt` see example I've provided. – Marek R May 03 '21 at 08:51

2 Answers2

1

Having a quick look at the Catch2 tutorial you are at least missing the specification of the Catch2 library to link. The tutorial states that you are at least need to have this code which your file above seems to miss:

find_package(Catch2 REQUIRED)
add_executable(tests test.cpp)
target_link_libraries(tests PRIVATE Catch2::Catch2)

The target_link_libraries... is missing in your example. That's probably why you are getting those linker errors. The find_package will introduce the library under the name Catch2::Catch2 which you can then link to your test executable. If the find_package command is not working in your setup, you may need to check how you did install Catch2.

FloWil
  • 432
  • 5
  • 15
  • Yep, this is same as example I've provided in comment (before `CMakeLists.txt` was shown). – Marek R May 03 '21 at 08:52
  • It still won't work for me. When I try to build it with CMake on the command line, it says By not providing "FindCatch2.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Catch2", but CMake did not find one. Could not find a package configuration file provided by "Catch2" with any of the following names: Catch2Config.cmake catch2-config.cmake" I don't know where to get those files. I thought that the header file from Catch2 would be enough to make the tests work ... – fel1-xd May 03 '21 at 09:25
  • For `find_package` to work you need to [install](https://github.com/catchorg/Catch2/issues/1383) Catch2 first. If you don't want to do that you can use `FetchContent` as described [here](https://github.com/catchorg/Catch2/blob/devel/docs/cmake-integration.md). – icebp May 03 '21 at 10:31
  • I tried to install Catch2 but it doesn't work. I cloned the repo and input the commands but it said "The C++ Compiler MinGW/bin/g++.exe is not able to compile a simple test program. – fel1-xd May 03 '21 at 10:43
  • See [this](https://stackoverflow.com/questions/65444608/mingw-bin-gcc-exe-is-not-able-to-compile-a-simple-test-program) question for the g++ problem. – icebp May 03 '21 at 11:01
0

I'm assuming that you want to use Catch2 as a header only library. The problem may be because you are including include/catch.hpp instead of single_include/catch2/catch.hpp.

icebp
  • 1,608
  • 1
  • 14
  • 24
  • I included the header "single_include/catch2/catch.hpp" but it still doesn't work – fel1-xd May 03 '21 at 10:26
  • How are you consuming Catch2? It's not clear from the question. Did you clone the repo and simply set the include path to `single_include/catch2`? – icebp May 03 '21 at 10:35
  • I just downloaded the header file and included it into my main.cpp – fel1-xd May 03 '21 at 10:50
  • And the error you're getting in this case is the same as the one in your original question? – icebp May 03 '21 at 11:04
  • Then maybe opening an issue on the Catch repository is the better option. – icebp May 03 '21 at 14:20