google / googletest

GoogleTest - Google Testing and Mocking Framework

Home Page:https://google.github.io/googletest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Building googletest using clang not gcc/g++

ronleeon opened this issue · comments

Running sample program linked to googletest library which was built with clang-11.0.0

Hello,I download the latest googletest source code(1.11.0) and build it using clang-11.0.0,
I use CMake(3.23.0-rc2) to build with no other configure args except CMAKE_INSTALL_PREFIX.
Everything is ok, and I make install to install the headers and libraries(libgtest,libgtest_main,libgmock,libgmock_main) to customized path.
Then I build a sample program following the tutorial, and the sample code is also:

// hello_test.cc

#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
  // Expect two strings not to be equal.
  EXPECT_STRNE("hello", "world");
  // Expect equality.
  EXPECT_EQ(7 * 6, 42);
}

And I found a bug,the program collapses,the error output is:

[build] [ 25%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[build] [ 50%] Building CXX object tests/CMakeFiles/hello_test.dir/hello_test.cc.o
[build] [ 75%] Linking CXX executable main
[build] [ 75%] Built target main
[build] [100%] Linking CXX executable hello_test
[build] /usr/bin/ld: /home/googletest/lib/x86_64-linux->gnu/libgtest.a(gtest-all.cc.o): relocation R_X86_64_32 against .rodata.str1.1' can not be used >when making a PIE object; recompile with -fPIE [build] /usr/bin/ld: /home/googletest/lib/x86_64-linux->gnu/libgtest_main.a(gtest_main.cc.o): warning: relocation in read-only section .text.startup'
[build] collect2: error: ld returned 1 exit status
[build] gmake[2]: *** [tests/CMakeFiles/hello_test.dir/build.make:97: tests/hello_test] Error 1
[build] gmake[1]: *** [CMakeFiles/Makefile2:126: tests/CMakeFiles/hello_test.dir/all] Error 2
[build] gmake: *** [Makefile:91: all] Error 2
[build] Build finished with exit code 2

However, when I transfer the toolchain from clang to g++(11.2.0) to build googletest and run the sample program,everything works fine and
the sample program runs correctly.

Steps to reproduce the bug

Just run the program with prebuilt googletest which is built by clang-11.0.0,
CMakeLists.txt is:

add_executable(hello_test hello_test.cc)
target_link_libraries(
	hello_test
	gtest
	gtest_main
)
# CMake has a builtin support for google test.
include(GoogleTest)
# Use gtest_discover_tests to discover a test
gtest_discover_tests(hello_test)

Does the bug persist in the most recent commit?

Maybe,but almost recent

What operating system and version are you using?

Ubuntu-21.10

What compiler and version are you using?

gcc -v:Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/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 11.2.0-7ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --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-11-ZPT0kp/gcc-11-11.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-ZPT0kp/gcc-11-11.2.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Ubuntu 11.2.0-7ubuntu2) 
clang -v:
```clang version 11.0.0 
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/llvm-11/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64

What build system are you using?

cmake --version:

cmake version 3.23.0-rc2

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

Additional context

No additional context.

InstalledDir: /home/llvm-11/bin

I think that's your problem. It seems that when you built clang it built in gtest libraries. Those libraries are then using internal llvm functions. There are references to gtest all over the llvm repository.

Here's a snippet of the errors I was getting (cmake hides some of them):

/bin/ld: gtest-all.cc:(.text._ZN7testing8internal25JsonUnitTestResultPrinter20TestPropertiesAsJsonERKNS_10TestResultERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x48a): undefined reference to `llvm::raw_os_ostream::~raw_os_ostream()'
/bin/ld: /data/git/llvm-project/build/bin/../lib/libgtest.a(gtest-all.cc.o): in function `testing::internal::ParseInt32(testing::Message const&, char const*, int*)':
gtest-all.cc:(.text._ZN7testing8internal10ParseInt32ERKNS_7MessageEPKcPi+0x85): undefined reference to `vtable for llvm::raw_os_ostream'
/bin/ld: gtest-all.cc:(.text._ZN7testing8internal10ParseInt32ERKNS_7MessageEPKcPi+0xa9): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'

It looks like it was using the gtest archive files in llvm-project/build/lib

I solved this problem by removing all references to gtest that were built by clang

#!/usr/bin/env fish
rm -r (find -iname '*gtest*')

I'm currently using clang 13.0.0 on this particular machine.