catchorg / Catch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
https://discord.gg/4CWS9zD
Boost Software License 1.0
18.72k stars 3.05k forks source link

v3: multiple linker errors when using libcxx #2768

Open p4vook opened 11 months ago

p4vook commented 11 months ago

Describe the bug When trying to compile any Catch2 v3 test case with libcxx as stdlib, multiple linker errors pop up.

Expected behavior I expected to be able to use libcxx as a drop-in replacement for libstdc++.

Reproduction steps Consider this testcase:

#include <catch2/catch_test_macros.hpp>

TEST_CASE() {
    REQUIRE(0 == 0);
}

Compile it with clang++, using libcxx as stdlib: clang++ -stdlib=libc++ -lCatch2 -lCatch2Main test.cpp

Get following errors:

/usr/bin/x86_64-pc-linux-gnu-ld.bfd: test-e75290.o: in function `Catch::BinaryExpr<int, int>::streamReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const':
test.cpp:(.text._ZNK5Catch10BinaryExprIiiE29streamReconstructedExpressionERNSt3__113basic_ostreamIcNS2_11char_traitsIcEEEE[_ZNK5Catch10BinaryExprIiiE29streamReconstructedExpressionERNSt3__113basic_ostreamIcNS2_11char_traitsIcEEEE]+0x70): undefined reference to `Catch::formatReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, Catch::StringRef, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)'
/usr/bin/x86_64-pc-linux-gnu-ld.bfd: /tmp/test-e75290.o: in function `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > Catch::Detail::stringify<int>(int const&)':
test.cpp:(.text._ZN5Catch6Detail9stringifyIiEENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERKT_[_ZN5Catch6Detail9stringifyIiEENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERKT_]+0x1e): undefined reference to `Catch::StringMaker<int, void>::convert(int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Platform information:

$ clang++ -v
clang version 17.0.5
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm/17/bin
Configuration file: /etc/clang/x86_64-pc-linux-gnu-clang++.cfg

libcxx version 17.0.5, Catch2 version 3.4.0 (Gentoo enables -DCATCH_DEVELOPMENT_BUILD=ON if that could be important).

p4vook commented 11 months ago

Could be related to #2728

horenmar commented 11 months ago

Catch2Main depends on Catch2. IIRC for bfd link order matters, so try that first.

p4vook commented 11 months ago

Same errors are produced even if I don't link to Catch2Main, so I assume it's not relevant.

chfast commented 10 months ago

The problem is the Catch2 must also be built with libc++. So you likely need to build it from source.

ChrisThrasher commented 10 months ago

The problem is the Catch2 must also be built with libc++. So you likely need to build it from source.

This is what I think may be happening as well.

p4vook commented 10 months ago

Hmm, is it possible to fix this?

Anyway, I think it should be mentioned somewhere in the documentation that Catch relies somewhat heavily on the standard library ABI. I couldn't find this mention anywhere, but I could've missed it though.

ChrisThrasher commented 10 months ago

Hmm, is it possible to fix this?

Sure it is. Compile Catch2 with libc++ and I expect it'd work.

I think it should be mentioned somewhere in the documentation that Catch relies somewhat heavily on the standard library ABI.

Pretty much all C++ libraries are susceptible to this. It's just one of those things you have to deal with when trying to use a non-default standard library for your given OS. I think it's outside the scope of Catch2's documentation to cover this.