-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Update CMake threads library detection #1602
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
Conversation
| Version: @VERSION@ | ||
|
|
||
| Libs: -L${libdir} -lbenchmark | ||
| Libs.private: -lpthread |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to keep this setting to Libs.private though please. pthread is only required to link benchmark, not to link against benchmark.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell (if I understand you correctly), -lpthread is required when linking against benchmark. Based off of that conclusion it should be redundant to be in both Libs.private and Libs.
I was only using pkg-config inside of a Makefile and my system (glibc 2.31) would not link the code that depends on -lbenchmark unless I added in -lpthreads myself. For example, when I build the demonstration file from the README using g++ demo.cpp `pkg-config --cflags --libs benchmark` -o demo with benchmarks v1.8.0 I get
/usr/bin/ld: /usr/local/lib/libbenchmark.a(benchmark_runner.cc.o): in function `benchmark::internal::BenchmarkRunner::DoNIterations()':
benchmark_runner.cc:(.text+0x1418): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
Edit: Anyone in future getting here from Google... add --static to pkg-config as described below or use a smarter build system 😜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libbenchmark.a is a static library, it itself isn't linked to the -lpthread (or to anything, really),
so naturally, when linking to the static library, you need to also link to all the libraries
said library would have linked against. This isn't the case for shared libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well that might explain why pkg-config --static --libs brings out -lpthread... Please hold.
|
|
||
| cxx_feature_check(STEADY_CLOCK) | ||
| # Ensure we have pthreads | ||
| set(THREADS_PREFER_PTHREAD_FLAG ON) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this being removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether -pthread or -lpthread is the right choice is not something I am knowledgeable enough to weigh in on. It's been discussed here a few times (#919 #771) and at googletest/#2482), too. I wanted to make as few decisions as possible, so I removed this to allow the default behaviour (@CMAKE_THREAD_LIBS_INIT@ will give -lpthread).
If these lines remained, I don't think there would be a way for someone to prefer -lpthread as THREADS_PREFER_PTHREAD_FLAG ON would always override it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm 99.99% sure this line should remain.
|
@LebedevRI @dmah42 I'm happy to close this PR. I've learned that if using I get the feeling it's best to keep things as they are (where you've had no complaints for quite some time) rather than change these flags in order to allow it to accidentally work correctly across both libc environments. |
Nice!
Yeah, i think the LHS of the diff isn't incorrect. |
When glibc < 2.34,
-lpthreador-pthreadneeds to be present in order for this project to be linked successfully. CMake provides the@CMAKE_THREAD_LIBS_INIT@variable that will expand to the necessary flag. This will typically expand to-lpthreads, but users can override this behaviour if they wish by setting theTHREADS_PREFER_PTHREAD_FLAGvariable to TRUE/ON/etc.This patch ensures the thread library is always present in the linker options and restores the default behaviour.