google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.8k stars 10.15k forks source link

[Bug]: undefined reference to `testing::internal::PrintU8StringTo` #4591

Open wangtz0607 opened 3 months ago

wangtz0607 commented 3 months ago

Describe the issue

The target fails to link with linker message "undefined reference to testing::internal::PrintU8StringTo" when std::u8string is used in tests.

Steps to reproduce the problem

Example

#include <string>

#include <gtest/gtest.h>

TEST(Example, Example) {
    std::u8string s = u8"foo", t = u8"foo";
    EXPECT_EQ(s, t);
}
target_compile_features(example PRIVATE cxx_std_20)
target_link_libraries(example PRIVATE GTest::gtest GTest::gtest_main)

Actual Behavior

The target fails to link with linker message:

/path/to/googletest/include/gtest/gtest-printers.h:704:(.text._ZN7testing8internal7PrintToERKNSt7__cxx1112basic_stringIDuSt11char_traitsIDuESaIDuEEEPSo[_ZN7testing8internal7PrintToERKNSt7__cxx1112basic_stringIDuSt11char_traitsIDuESaIDuEEEPSo]+0x1f): undefined reference to `testing::internal::PrintU8StringTo(std::__cxx11::basic_string<char8_t, std::char_traits<char8_t>, std::allocator<char8_t> > const&, std::ostream*)'

Expected Behavior

The target should link normally.

What version of GoogleTest are you using?

Tag v1.15.2

What operating system and version are you using?

$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo

What compiler and version are you using?

$ g++ -v
Using built-in specs.
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 13.2.0-23ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-13/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-13 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-13-uJ7kn6/gcc-13-13.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-13-uJ7kn6/gcc-13-13.2.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.0 (Ubuntu 13.2.0-23ubuntu4)

What build system are you using?

$ cmake --version
cmake version 3.29.0

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Additional context

No response

aidtopia commented 2 months ago

Your project is set to use C++20 (which is necessary for std::u8string) but I'm guessing your googletest library was built against an older C++ standard, like C++14 or C++17.

See https://github.com/google/googletest/issues/4528

I've had the same problem for a while and just this morning resolved it by figuring out how to rebuild the googletest libraries using C++20.