Search code examples
c++catch2

Catch2 undefined reference to Catch::StringMaker


I am using Catch2 from the actual devel branch and I have a linker error when trying to compare two vector<string_view>

The issue can be reproduced as following:

#include <vector>
#include <string_view>
#include <catch2/catch_test_macros.hpp>

TEST_CASE("Can split string", "[split]") {
  vector<string_view> splittedString = {"this is a string view"sv};
  vector<string_view> EXPECTED = {"I'm"sv, "Elvis"sv,"Dukaj"sv};
  REQUIRE(splittedString == EXPECTED);
}

My CMakeLists.txt:

find_package(Catch2 REQUIRED)
# ...
target_link_libraries(${PROJECT_NAME} PRIVATE  Catch2::Catch2WithMain)

When trying to build I have the following linker error:

Catch2/src/catch2/../catch2/catch_tostring.hpp:126: error: undefined reference to `Catch::StringMaker<std::basic_string_view<char, std::char_traits<char> >, void>::convert[abi:cxx11](std::basic_string_view<char, std::char_traits<char> >)'

What am I doing wrong? Am I missing some includes?


Solution

  • Solution

    You need to build Catch with C++17 support to be able to use string_view inside REQUIRE() and other macros. By default, Catch is built with C++14 for some reason.

    To do so you can run this in directory with your Catch sources:

    mkdir build && cd build
    cmake -DCMAKE_CXX_STANDARD=17 ..
    sudo make install
    

    Notes

    1. You are heavily relying on Catch2 v3 features, so you should use find_package(Catch2 3 REQUIRED) instead of just find_package(Catch2 REQUIRED) in your cmake file
    2. You can see discussions of the issue here and here