Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linker error compiling with clang on linux #3878

Open
adam-azarchs opened this issue Jun 8, 2022 · 2 comments · May be fixed by #3879
Open

Linker error compiling with clang on linux #3878

adam-azarchs opened this issue Jun 8, 2022 · 2 comments · May be fixed by #3879
Labels

Comments

@adam-azarchs
Copy link

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

googletest/BUILD.bazel

Lines 123 to 125 in 0e40217

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

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 added a commit to adam-azarchs/googletest that referenced this issue Jun 8, 2022
Not all compilers on linux add it implicitly.
It won't do any harm on those which do.

Closes google#3878
@adam-azarchs adam-azarchs linked a pull request Jun 8, 2022 that will close this issue
@adam-azarchs
Copy link
Author

adam-azarchs commented Jun 8, 2022

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.

@yesudeep
Copy link
Contributor

yesudeep commented Jan 4, 2023

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.

  • 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
  • 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants