Catch2 undefined reference to Catch::StringMaker
Asked Answered
M

1

7

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?

Macnamara answered 16/2, 2021 at 15:23 Comment(7)
I think matchers were split to separate header, try including catch2/catch_matchers_all.hpp as well.Nudicaul
"What am I doing wrong?" - why are you building from their develop branch? That's not intended for production use.Lichi
@Lichi this is not production code :) Anyway I switched to v2.x branch and chaneged the include with #include <catch2/catch.hpp> and I have still the same problem.Macnamara
@Nudicaul there is no such header file in Catch2 neither from v2 and devel branchMacnamara
That header exists in the sources, hm. Anyway, I recall having a problem with std::string_view matcher as well. I think it was limited to C++17 in the Catch2 sources and I was using some combination like Clang on Windows that was not recognized by that.Nudicaul
@Lichi anyw the develop branch is the old master so is something that is super stable in theoryMacnamara
@Nudicaul I added also #include <catch2/matchers/catch_matchers_all.hpp> but the problem is still here.Macnamara
C
9

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
Coral answered 12/12, 2021 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.