4

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?

Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
  • I think matchers were split to separate header, try including `catch2/catch_matchers_all.hpp` as well. – Resurrection Feb 16 '21 at 15:26
  • 2
    "What am I doing wrong?" - why are you building from their `develop` branch? That's not intended for production use. – Dai Feb 16 '21 at 15:26
  • @Dai this is not production code :) Anyway I switched to `v2.x` branch and chaneged the include with `#include ` and I have still the same problem. – Elvis Dukaj Feb 16 '21 at 15:35
  • @Resurrection there is no such header file in Catch2 neither from v2 and devel branch – Elvis Dukaj Feb 16 '21 at 15:37
  • 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. – Resurrection Feb 16 '21 at 15:56
  • @Dai anyw the `develop` branch is the old `master` so is something that is super stable in theory – Elvis Dukaj Feb 16 '21 at 16:12
  • @Resurrection I added also `#include ` but the problem is still here. – Elvis Dukaj Feb 16 '21 at 16:22

1 Answers1

6

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
Вовка
  • 176
  • 2
  • 5