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

[bazel build] Linking error due to not linking with pthread #176

Closed
robryk opened this issue Oct 7, 2016 · 6 comments
Closed

[bazel build] Linking error due to not linking with pthread #176

robryk opened this issue Oct 7, 2016 · 6 comments
Labels
Milestone

Comments

@robryk
Copy link

robryk commented Oct 7, 2016

When I compile gflags-example (as well as my own project), I get the following error:


robryk@iguatalpa:~/example$ bazel build ...
.
INFO: Found 1 target...
ERROR: /usr/local/google/home/robryk/example/foo/BUILD:1:1: Linking of rule '//foo:foo' failed: linux-sandbox failed: error executing command /usr/local/google/home/robryk/.cache/bazel/_bazel_robryk/0d0280d3a76251d432180c833ad92a5c/execroot/example/_bin/linux-sandbox ... (remaining 13 argument(s) skipped).
bazel-out/local-fastbuild/bin/external/gflags_git/libgflags.a(gflags.pic.o): In function `gflags_mutex_namespace::Mutex::Mutex()':
gflags.cc:(.text._ZN22gflags_mutex_namespace5MutexC2Ev[_ZN22gflags_mutex_namespace5MutexC5Ev]+0x39): undefined reference to `pthread_rwlock_init'
bazel-out/local-fastbuild/bin/external/gflags_git/libgflags.a(gflags.pic.o): In function `gflags_mutex_namespace::Mutex::Mutex(gflags_mutex_namespace::Mutex::LinkerInitialized)':
gflags.cc:(.text._ZN22gflags_mutex_namespace5MutexC2ENS0_17LinkerInitializedE[_ZN22gflags_mutex_namespace5MutexC5ENS0_17LinkerInitializedE]+0x3c): undefined reference to `pthread_rwlock_init'
bazel-out/local-fastbuild/bin/external/gflags_git/libgflags.a(gflags.pic.o): In function `gflags_mutex_namespace::Mutex::~Mutex()':
gflags.cc:(.text._ZN22gflags_mutex_namespace5MutexD2Ev[_ZN22gflags_mutex_namespace5MutexD5Ev]+0x2c): undefined reference to `pthread_rwlock_destroy'
bazel-out/local-fastbuild/bin/external/gflags_git/libgflags.a(gflags.pic.o): In function `gflags_mutex_namespace::Mutex::Lock()':
gflags.cc:(.text._ZN22gflags_mutex_namespace5Mutex4LockEv[_ZN22gflags_mutex_namespace5Mutex4LockEv]+0x20): undefined reference to `pthread_rwlock_wrlock'
bazel-out/local-fastbuild/bin/external/gflags_git/libgflags.a(gflags.pic.o): In function `gflags_mutex_namespace::Mutex::Unlock()':
gflags.cc:(.text._ZN22gflags_mutex_namespace5Mutex6UnlockEv[_ZN22gflags_mutex_namespace5Mutex6UnlockEv]+0x20): undefined reference to `pthread_rwlock_unlock'
collect2: error: ld returned 1 exit status
Target //foo:foo failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 3.682s, Critical Path: 0.18s

Adding linkopts = [ "-lpthread" ] to the binary target I'm compiling works the problem around for me.

@schuhschuh
Copy link
Member

Thanks, I've noted this also in #153, but had no time yet to look into how to fix this properly for both OS X (no need for -lpthread) and Linux (requires -lpthread):

I've just tested it on both OS X 10.11 and Ubuntu 14.04. On Ubuntu, I needed to add pthreads to the link options. These result in a clang warning of an unused option on OS X, though. I am not clear yet how to conditionally add this link option only for Linux.

@robryk
Copy link
Author

robryk commented Oct 7, 2016

Would you mind sharing the warning that you get on OS X? For librt a POSIX-compliant compiler has to accept -lrt, even if librt is part of libc on a particular system. I'm not sure if it's not also the case for libpthread. If so, we might wish to just stomach the warning for now and file a bug against Clang.

@schuhschuh
Copy link
Member

schuhschuh commented Oct 7, 2016

I'll have a look and will post it.

BTW: There's a branch with the fix where I was preparing a PR in my fork.

@schuhschuh
Copy link
Member

Looking at it, I used -pthread instead of -lpthread... maybe that's the issue for clang?

@robryk
Copy link
Author

robryk commented Oct 7, 2016

Yes, POSIX specifies that a compiler has to understand -lpthread:

http://pubs.opengroup.org/onlinepubs/9699919799//utilities/c99.html (grep for "-l pthread")

It also seems that POSIX doesn't say anything about -pthread, while GCC manual says that this impacts both the preprocessor (?) and linker.

@schuhschuh
Copy link
Member

Alright, thanks for clarifying. I'll change it to -lpthread then and push.

@schuhschuh schuhschuh added this to the v2.2.0 milestone Nov 23, 2016
agl pushed a commit to google/boringssl that referenced this issue Jan 6, 2023
This seems to work more reliably across platforms. Actually, both seem
to work on Apple platforms, so we probably didn't need the conditions,
while Android seems to only accept -pthread, based on [1].

This also matches Abseil [2], Envoy [3], and a random example in Bazel
documentation [4].

Though, as a counter-example, POSIX seems to prefer -lpthread, not
-pthread, per [5].

[1] grpc/grpc#31938 (comment)
[2] https://github.com/abseil/abseil-cpp/blob/625a18016d6208c6c0419697cb6caa3f23ce31bc/absl/base/BUILD.bazel#L185
[3] https://github.com/envoyproxy/envoy/blob/main/bazel/envoy_binary.bzl#L72
[4] https://bazel.build/tutorials/cpp-use-cases#include-external-libraries
[5] gflags/gflags#176 (comment)

Change-Id: I15005bcf4e4b7ebe91a835b8262c5c6434715c3b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55927
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
nebeid pushed a commit to nebeid/aws-lc that referenced this issue Jan 23, 2023
This seems to work more reliably across platforms. Actually, both seem
to work on Apple platforms, so we probably didn't need the conditions,
while Android seems to only accept -pthread, based on [1].

This also matches Abseil [2], Envoy [3], and a random example in Bazel
documentation [4].

Though, as a counter-example, POSIX seems to prefer -lpthread, not
-pthread, per [5].

[1] grpc/grpc#31938 (comment)
[2] https://github.com/abseil/abseil-cpp/blob/625a18016d6208c6c0419697cb6caa3f23ce31bc/absl/base/BUILD.bazel#L185
[3] https://github.com/envoyproxy/envoy/blob/main/bazel/envoy_binary.bzl#L72
[4] https://bazel.build/tutorials/cpp-use-cases#include-external-libraries
[5] gflags/gflags#176 (comment)

Change-Id: I15005bcf4e4b7ebe91a835b8262c5c6434715c3b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55927
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
(cherry picked from commit 412b20b0f699938a370a97cc11815b1eb1e0fcb2)
nebeid pushed a commit to nebeid/aws-lc that referenced this issue Jan 24, 2023
This seems to work more reliably across platforms. Actually, both seem
to work on Apple platforms, so we probably didn't need the conditions,
while Android seems to only accept -pthread, based on [1].

This also matches Abseil [2], Envoy [3], and a random example in Bazel
documentation [4].

Though, as a counter-example, POSIX seems to prefer -lpthread, not
-pthread, per [5].

[1] grpc/grpc#31938 (comment)
[2] https://github.com/abseil/abseil-cpp/blob/625a18016d6208c6c0419697cb6caa3f23ce31bc/absl/base/BUILD.bazel#L185
[3] https://github.com/envoyproxy/envoy/blob/main/bazel/envoy_binary.bzl#L72
[4] https://bazel.build/tutorials/cpp-use-cases#include-external-libraries
[5] gflags/gflags#176 (comment)

Change-Id: I15005bcf4e4b7ebe91a835b8262c5c6434715c3b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55927
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
(cherry picked from commit 412b20b0f699938a370a97cc11815b1eb1e0fcb2)
nebeid pushed a commit to nebeid/aws-lc that referenced this issue Jan 25, 2023
This seems to work more reliably across platforms. Actually, both seem
to work on Apple platforms, so we probably didn't need the conditions,
while Android seems to only accept -pthread, based on [1].

This also matches Abseil [2], Envoy [3], and a random example in Bazel
documentation [4].

Though, as a counter-example, POSIX seems to prefer -lpthread, not
-pthread, per [5].

[1] grpc/grpc#31938 (comment)
[2] https://github.com/abseil/abseil-cpp/blob/625a18016d6208c6c0419697cb6caa3f23ce31bc/absl/base/BUILD.bazel#L185
[3] https://github.com/envoyproxy/envoy/blob/main/bazel/envoy_binary.bzl#L72
[4] https://bazel.build/tutorials/cpp-use-cases#include-external-libraries
[5] gflags/gflags#176 (comment)

Change-Id: I15005bcf4e4b7ebe91a835b8262c5c6434715c3b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55927
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
(cherry picked from commit 412b20b0f699938a370a97cc11815b1eb1e0fcb2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants