- link to pthread library using CMake (in CLion)
- 5 Answers 5
- Not the answer you’re looking for? Browse other questions tagged c cmake pthreads or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Building error using cmake: cannot find -lpthreads
- 9 Answers 9
- Hint: search for the actual error message
- Cmake error undefined reference to `pthread_create’
- 2 Answers 2
- cmake cannot find pthread #9
- Comments
- ekg commented Feb 14, 2018
- hannespetur commented Feb 14, 2018
- ekg commented Feb 14, 2018
- ekg commented Feb 14, 2018 •
- hannespetur commented Feb 14, 2018 •
- ekg commented Feb 14, 2018
- hannespetur commented Feb 14, 2018
- ekg commented Feb 14, 2018
- ekg commented Feb 14, 2018
- ekg commented Feb 14, 2018
- ekg commented Feb 14, 2018
- hannespetur commented Feb 14, 2018 •
- Using the target_link_libraries() statement to control the linked libraries
link to pthread library using CMake (in CLion)
I’ve looked all over and I can’t figure out how to get CLion to link the lpthread library. I know that w/ gcc you can just type -lpthread, but I need to do some debugging in CLion.
Here’s my current CMakeLists file:
5 Answers 5
Before CMake 2.8.12:
If you have CMAKE 2.8.12+:
If you have CMake 3.1.0+
If you want to use one of the first two methods with CMake 3.1+, you will need:
Answer as of CLion 2018.2 and the bundled cmake version of 3.12.0
I used other answers in this thread to modify my CMakeLists.txt, and ultimately found I had to add a SECOND line with set() to make this work. My file looks like the following:
Not the answer you’re looking for? Browse other questions tagged c cmake pthreads or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Building error using cmake: cannot find -lpthreads
I have c++ project that was smoothly running on a given machine, and now I am trying to compile it on another one with the same operating system (Xubuntu 14.04).
I’ve installed all the dependencies and I’am using cmake to build the project, although it stops with the following error:
Determining if the function pthread_create exists in the pthreads failed with the following output: . /usr/bin/ld: cannot find -lpthreads
The cmakelists.txt lines that include the compiler flags are as follows:
I have done some research and have already tried the following:
-used -pthread/-threads/-thread/-lpthreads instead of -lpthread, which does not solve the issue and makes the build stop without finding the following package: find_package (Threads)
- changed the order of -lpthread in the cmakelists line above, which gives the same error
- used different versions o gcc/g++: tried 4.4, 4.6 and 4.8, without any change
- created a symbolic link to libpthread.so in /usr/lib/, without any change
I would appreciate some help, since I am already short on ideas on what to try next.
Edit 1
The library is where it should:
The pthread_create is also found:
I have also verified that both libpthread-stubs0 and libc6-dev are present.
Edit 2
This is part of the FindThreads.cmake file content, located in /usr/share/cmake-2.8/Modules/:
Edit 3
Used a minimal Cmakelists.txt as follows:
Which produced the following output:
9 Answers 9
The problem was happening when running cmake. Though, in this case cmake was not the problem the error was silent and the -lpthreads related error/warning was the only thing being written to the cmake error log file, although that was not causing any issue. I’ve done a minimal version of the cmakelists.txt and started testing it line by line until I found which package was causing it to stop: finally I found it was a version mismatch.
Hint: search for the actual error message
Typically you’d look for the last error message. However, this (often useful) strategy in such cases leads astray.
What you are looking at is the CMakeCache.txt , the CMakeOutput.log or the CMakeError.log . How comes? When some of the macros or tests in the configure phase fails, CMake «helpfully» dumps these files to the output. Unfortunately, these files can be thousands of lines long, and typically contain lots of » *** Error: xyz» entries, for various configure checks. The one for » -lpthreads » just accidentally happened to be the last one in the log.
Solution: go through the log from the top, identify the section with the configure checks, find the last configure check prior to the point, where CMake identifies failure and dumps its logs. You might also try so search for the text » Configuring incomplete, errors occurred! «
Typically you’ll either find a very precise actual error message there, or at least you find the name / path of the macro or function called last, and this allows you to pinpoint down what actually went wrong.
Источник
Cmake error undefined reference to `pthread_create’
I make a test for cmake FindThreads. Here is my source code test.cpp and CMakeLists.txt:
I get the outpu:
then I check CMakeError.txt, find that:
My question is why performing Test CMAKE_HAVE_LIBC_PTHREAD — Failed, and since it failed, did it really find Threads, I am totally confused. Thanks for any replying!
2 Answers 2
There is a little reason to examine CMakeError.txt until CMake reports about not-working compiler or CMake reports about unavailable feature, which is detected by probe compilation/linking, but you expect that feature to be available.
In your case you have successful CMake configuration (look at the last lines in CMake output), and you have successfully detected Threads-related library(see below). No reasons to worry and no reasons to look into CMakeError.txt .
Yes, Threads are found. E.g., CMake clearly states
Other ways for deduce, that Threads has been found:
You use REQUIRED keyword with find_package(Threads) . Would Threads not found, CMake will report about an error and terminate configuration.
You may check Threads_FOUND variable after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).
You may check Threads::Threads target after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).
FindThreads.cmake tries it best to determine if the compiler supports pthreads as a library, a compile time switch, or a link time switch, etcetera.
The failure you are seeing is if a pthreads library exists. It doesn’t. Instead you are supposed to compile the files with -pthreads which is a compile time switch your compiler does accept. This is align with the clang documentation which shows that -pthreads is a compiler option, not a linker option. https://clang.llvm.org/docs/ClangCommandLineReference.html#compilation-flags
So, did it find Threads? Not exactly, there is nothing to find. Did it determine how to use pthreads? Yes.
Источник
cmake cannot find pthread #9
Comments
ekg commented Feb 14, 2018
I’m having trouble building graphtyper:
It seems that cmake can’t find pthread, but I apparently have it on my system. Has anyone else run into this problem?
The text was updated successfully, but these errors were encountered:
hannespetur commented Feb 14, 2018
I haven’t seen this problem before. Seems to be CMake related issue since the CheckSymbolExists.c file is generated by CMake. Which version of cmake are you using? I want to see if I can reproduce the problem with the same version.
All the best,
Hannes
ekg commented Feb 14, 2018
The CheckSymbolExists.c file contains:
Indeed, I cannot compile it using the command that CMake is giving:
I reproduce this:
But I can compile it as follows:
Why would it be looking for lpthreads ? That doesn’t seem to be the correct name of the library. This is relevant: https://stackoverflow.com/questions/37967086/usr-bin-ld-cannot-find-lpthreads. I’ll dig around in the CMakeLists.txt to see where it’s defined this way.
ekg commented Feb 14, 2018 •
lpthreads seems to be referred to in ./sparsehash/m4/acx_pthread.m4. Is that somehow used during the cmake setup? It’s apparently a solaris thing.
Maybe the setup in CMakeLists.txt is not specific enough:
For clarity, I’m not on solaris, but apparently ubuntu precise. (I’m not the sysadmin.)
hannespetur commented Feb 14, 2018 •
Thank you for looking into this thoroughly. No /sparsehash/m4/acx_pthread.m4 should not be used during cmake setup. I installed cmake3.3-rc2 but I was able to build graphtyper without problems using that version. However, looking into CMakeFiles/CMakeError.log I see similar errors as you:
I checked in my original build directory and these errors also appear there. So it seems they are excpected.
What I think is happening internally in CMake when find_packages(Threads) is called is that it is checking whether it should use the flag -lpthread , -lpthreads or simply skip the flag in case it is not needed. Both skipping the flag and using -lpthreads resulted in errors which are shown in CMakeError.log.
Are there any other errors in your CMakeError.log? If not, maybe the configuring was successful and you are able to run make in the release-build directory.
All the best,
Hannes
ekg commented Feb 14, 2018
This is the only error, and yet I am not able to run make. the makefile has not been made.
Which versions of CMake work? It looks like this might be a bug in the specific release candidate I’m using and possibly I should upgrade or downgrade.
hannespetur commented Feb 14, 2018
I am using CMake 3.8.2 on my personal computer and 2.8.12 at work, these two should at least work. But would be surprised if that is problem since I was able to use CMake 3.3-rc2 without any problems. Does the output of the cmake .. not reveal anything unusual? Here is my output when using CMake 3.3-rc2
ekg commented Feb 14, 2018
Here’s my cmake output;
ekg commented Feb 14, 2018
Ah, sorry, there is also the issue of the boost libraries. I have them in a custom location so I need to tell cmake where to look for them. Let me see if that helps.
ekg commented Feb 14, 2018
OK! This appears to have resolved the problem:
Excuse the noise! I’m compiling now.
ekg commented Feb 14, 2018
And things may still be complicated as I’d made modifications to the CMakeLists.txt. I’ll post if setting the boost root isn’t enough to resolve the problem. Otherwise I’ll close the issue.
hannespetur commented Feb 14, 2018 •
Great to see that you figured out the problem. Let me know if you encounter any other issues.
Источник
Using the target_link_libraries() statement to control the linked libraries
This tutorial shows various ways to use CMake’s target_link_libraries() statement and explains the differences between them. We will create a basic Raspberry Pi project using the sqlite3 library and will then show different ways of using target_link_libraries() to reference the necessary libraries.
In this tutorial we will use Visual Studio and VisualGDB, however the steps described below will work with any other CMake-based environment.
- Connect to your Raspberry Pi via SSH and install the libsqlite3-dev package. If you are using SmarTTY, the Tools->Manage Linux Packages command can help you find the exact package name easier:
- Start Visual Studio and open the VisualGDB Linux Project Wizard:
- On the first page select “Create Application -> CMake -> Ninja”:
- On the next page select your Raspberry Pi cross-toolchain and pick the SSH connection for your Pi. Ensure the “test connection” checkbox is checked so that VisualGDB can check that all the necessary packages are present:
- Press “Finish” to create the project. Once it is created, open VisualGDB Project Properties and click “Synchronize Sysroot” on the CMake project settings page. This will copy the sqlite libraries from the Raspberry Pi into the toolchain so that you can reference them from the projects built with that toolchain:
- Replace the contents of the main source file with the following basic project demonstrating the use of sqlite3:
Источник