googletest icon indicating copy to clipboard operation
googletest copied to clipboard

Linker error compiling with clang on linux

Open adam-azarchs opened this issue 3 years ago • 2 comments

When compiling tests with bazel with clang on linux, I get a linker error undefined reference to nextafter.

Does the bug persist in the most recent commit?

Yes.

What operating system and version are you using?

Linux. The specifics don't matter because we're using a custom sysroot, described below.

What compiler and version are you using?

clag version 13.0.0 with --sysroot pointing to a sysroot built using crosstool-ng based on centos7.

What build system are you using?

bazel version 5.1.1.

Additional context

This is basically the same bug as #3601, but the fix for that one made the incorrect assumption that this was only a problem on BSD. The correct solution is to change https://github.com/google/googletest/blob/0e402173c97aea7a00749e825b194bfede4f2e45/BUILD.bazel#L123-L125 to be simply

    "//conditions:default": ["-lm", "-pthread"], 

It won't do any harm when using compilers like gcc which add -lm implicitly, but it will prevent breakage for compilers which do not do this.

adam-azarchs avatar Jun 08 '22 00:06 adam-azarchs

Actually slight correction here: gcc isn't implicitly linking -lm, but it is using a builtin implementation for nextafter so that it doesn't require -lm. At least our build of clang (which, granted, is customized for our needs) does not do that.

adam-azarchs avatar Jun 08 '22 00:06 adam-azarchs

The problem is very likely rules_cc using clang instead of clang++ as the C++ compiler in its toolchain definition. AFAIK, clang is the C compiler driver, not the C++ compiler driver.

  • [x] clang
>> /usr/bin/clang++ -o bazel-out/freebsd-fastbuild/bin/dummy_test -Xlinker -rpath -Xlinker '$ORIGIN/_solib_freebsd/' -Xlinker -rpath -Xlinker '$ORIGIN/dummy_test.runfiles/_main/_solib_freebsd/' -Lbazel-out/freebsd-fastbuild/bin/_solib_freebsd bazel-out/freebsd-fastbuild/bin/_objs/dummy_test/dummy_test.pic.o -lexternal_Sgoogletest~1.11.0_Slibgtest_Umain -lexternal_Sgoogletest~1.11.0_Slibgtest -pthread -Wl,-S -lstdc++ -Wl,-z,relro,-z,now -no-canonical-prefixes
  • [x] clang++
>> /usr/bin/clang -o bazel-out/freebsd-fastbuild/bin/dummy_test -Xlinker -rpath -Xlinker '$ORIGIN/_solib_freebsd/' -Xlinker -rpath -Xlinker '$ORIGIN/dummy_test.runfiles/_main/_solib_freebsd/' -Lbazel-out/freebsd-fastbuild/bin/_solib_freebsd bazel-out/freebsd-fastbuild/bin/_objs/dummy_test/dummy_test.pic.o -lexternal_Sgoogletest~1.11.0_Slibgtest_Umain -lexternal_Sgoogletest~1.11.0_Slibgtest -pthread -Wl,-S -lstdc++ -Wl,-z,relro,-z,now -no-canonical-prefixes
ld: error: bazel-out/freebsd-fastbuild/bin/_solib_freebsd/libexternal_Sgoogletest~1.11.0_Slibgtest.so: undefined reference to nextafter [--no-allow-shlib-undefined]
clang: error: linker command failed with exit code 1 (use -v to see invocation)

yesudeep avatar Jan 04 '23 07:01 yesudeep