I would like to compile gtkmm programs that use mutithreading. I used this example to test multithreading.
Unfortunately I get some linking errors when I compile:
/usr/bin/ld: src/multithreadingandprogress.p/multithreadingandprogress-window.cpp.o: undefined reference to symbol 'pthread_create##GLIBC_2.2.5'
and:
/usr/bin/ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
I guess I need some LDFLAGS or some environment variables to set in the settings?? Is there anyone who knowes exactly how to set up gnome builder for this task to compile?
Any help? Thx
I had similar issues while building the same tutorial in gtkmm-3.0. Adding the following line in the CMake file fixed the issue.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
If you are not using CMake you need to add this flag for your compiler manually.
The complete CMake for using threads in linux systems must contain following commands:
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
Check this link for a more complete approach with CMake.
Related
After reading a lot of SO questions on the matter, I just couldn't get it to work. I downloaded boost_1_60_0, then I ran the commands to build it:
sudo ./bootstrp.sh --prefix=/home/ricardo/boostlib
sudo ./b2 install -j8
I even tried running b2 like this:
sudo ./b2 install -j8 architecture=x86 address-model=64
Does not matter. The error is always the same:
main.cpp:(.text+0x7e): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x8a): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x96): undefined reference to `boost::system::system_category()'
Yeah, I know. Linking error, should run with -lboost_system and all. Yeah, you should put -L/home/ricardo/boostlib/lib. I know.
This is what my CMake looks like:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/home/ricardo/boostlib/lib -lboost_system -Wl,--verbose")
include_directories("/home/ricardo/boostlib/include")
You might be asking: What's the output of -Wl,--verbose? Well, surprisingly enough, libboost_system.so has been found:
attempt to open /home/ricardo/boostlib/lib/libboost_system.so succeeded
-lboost_system (/home/ricardo/boostlib/lib/libboost_system.so)
Okay. The library was found. I'm still trying to find some alternatives, nothing seems to work so far. So, even though I'm linking it against boost, and boost_system has been found, the program still does not compile due to... why? Can someone explain me?
Edit: How to link C++ program with Boost using CMake seems to be working. Though I still would like to know why my method does not work, and what should I do when I want to link against boost using just the g++ compiler, without CMake and Make. I've been able to link against Openblas succesfully before, so I wonder why it isn't working with Boost.
Edit2: This is the g++ command I got after running make VERBOSE=1, now I can see that the sources are being put AFTER the dependencies.
-std=c++11 -L/home/ricardo/boostlib/lib -lboost_system CMakeFiles/prophet-service.dir/main.cpp.o -o prophet-service -rdynamic
And this is my current CMake file:
cmake_minimum_required(VERSION 3.2)
project(prophet-service)
set(SOURCE_FILES
main.cpp)
include_directories("/home/ricardo/boostlib/include")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/home/ricardo/boostlib/lib -lboost_system")
add_executable(prophet-service ${SOURCE_FILES})
It also seems that I don't have the libbost_system.a file. At least now I know that I need the .a file instead of the .so file.
Are you linking in the right order? The thing that HAS the dependency needs to go before the thing that SATISFIES the dependency.
You need to link version of the library with extension .a because linker requires this extension (used for static libraries) and not .so which is used for shared libraries (the same of dll for Windows).
For example, if I search for libboost_system.a on my Ubuntu operating system I find the following:
frar#Home-PC:~$ locate libboost_system.a
/home/frar/Documents/SVILUPPO/boost_1_59_0/bin.v2/libs/system/build/gcc-4.8/release/link-static/threading-multi/libboost_system.a
/home/frar/Documents/SVILUPPO/boost_1_59_0/stage/lib/libboost_system.a
/usr/lib/x86_64-linux-gnu/libboost_system.a
I installed the libtins package for C++, by building it as described and adding #include <tins/tins.h> to the header of the example file. I ran ldconfig but building always throws a Undefined reference error:
CMakeFiles/lts.dir/main.cpp.o: In function `main':
/home/patrick/ClionProjects/lts/main.cpp:6: undefined reference to `Tins::EthernetII::EthernetII(Tins::HWAddress<6ul, unsigned char> const&, Tins::HWAddress<6ul, unsigned char> const&)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IP::IP(Tins::IPv4Address, Tins::IPv4Address)'
/home/patrick/ClionProjects/lts/main.cpp:8: undefined reference to `Tins::TCP::TCP(unsigned short, unsigned short)'
/home/patrick/ClionProjects/lts/main.cpp:10: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
/home/patrick/ClionProjects/lts/main.cpp:12: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
CMakeFiles/lts.dir/main.cpp.o: In function `Tins::EthernetII::~EthernetII()':
/usr/local/include/tins/ethernetII.h:46: undefined reference to `vtable for Tins::EthernetII'
/usr/local/include/tins/ethernetII.h:46: undefined reference to `Tins::PDU::~PDU()'
collect2: error: ld returned 1 exit status
make[3]: *** [lts] Error 1
I am using CLion as IDE and the following cmake file:
cmake_minimum_required(VERSION 3.5)
project(lts)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -ltins")
set(SOURCE_FILES main.cpp)
add_executable(lts ${SOURCE_FILES})
Any Idea what could be the problem?
There's a couple of things to address in your question. First, let CMake find the location of the libtins library for you, then use target_link_libraries() to add it to your lts executable. In the CMake sample in your question, you are specifying library options in the compiler flags variable (CMAKE_CXX_FLAGS). The other thing is to also let CMake handle setting the appropriate compiler flags for C++11 instead of manually adding them to CMAKE_CXX_FLAGS (which would only take care of the compiler part but not the linker). The following should do what you want in a robust, platform independent way:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(lts)
find_library(TINS_LIBRARY tins)
set(SOURCE_FILES main.cpp)
add_executable(lts ${SOURCE_FILES})
target_link_libraries(lts "${TINS_LIBRARY}")
I recommend you read the CMake docs for find_library() and target_link_libraries() to understand what those commands do. For the C++11 stuff, I recommend this blog article for a more cohesive explanation than what the CMake docs give you (disclosure: I wrote the article).
The CMake variable CMAKE_CXX_FLAGS is for the compiler flags, not for libraries.
To add a library you should use target_link_libraries:
target_link_libraries(lts tins)
If you want all targets to link to the same library, you should add it to the list CMAKE_STANDARD_LIBRARIES.
A small tip on debugging these kind of things: Either set CMAKE_VERBOSE_MAKEFILE or use make VERBOSE=1 to inhibit the normal CMake build output, and instead display the normal make output, which will show you all commands make runs, and all flags and options used for each and every program. That way you will be able to see the -ltins flag being passed as a compiler flag, but not as a linker flag or library.
The compiler should -ltins. Refer to CMake manual on how to add library dependencies.
From the Download page of libtins:
In order to link your application with libtins on GCC or clang, use the -ltins flag
Simply add the following line in your CMake file:
target_link_libraries(PROJECT_NAME -ltins)
I have Debian Wheezy and I need C++11 features to build my project. I've compiled gcc(c and c++ only) from source and put the libraries under my home folder using this question. I am also using the wrapper script supplied by the guy who answered that question which is
$HOME/gcc/gcc-4.8.4/bin/g++ -Wl,-rpath,$HOME/gcc/gcc-4.8.4/lib32 "$#"
Now, I have a game engine that I use in my project for GUI and graphic operations. It compiles in 32 bit just fine. I pass -m32 switch for all of the external libraries it uses and the engine itself. Also, in order for cmake to find my wrapper, I give following options while running cmake
cmake .. -DCMAKE_CXX_COMPILER=path/to/my/32-bit-wrapper
After compilation, it gives following linking erros
undefined reference to `XOpenDisplay'
undefined reference to `glBlendFunc'
undefined reference to `alGenBuffers'
At first, I thought I may be missing the 32-bit development libraries, so I installed following packages.
libgl1-mesa-dev:i386
libopenal-dev:i386
libx11-dev:i386
but I am getting errors, regardless. So, How can I solve this problem? I can supply additional information such as cmake files etc if needed. Thank you in advance.
EDIT
Cmake file in case if error stems from it
cmake_minimum_required(VERSION 2.8.3)
project(uwmf)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m32 -DLINUX")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 -DLINUX")
#set(CMAKE_CXX_COMPILER "${HOME_PATH}/devel/g++-4.8.4-32")
#set(CMAKE_C_COMPILER "${HOME_PATH}/devel/gcc-4.8.4-32")
message("${CMAKE_CXX_FLAGS}")
message("${CMAKE_C_FLAGS}")
message("${CMAKE_CXX_COMPILER}")
message("${CMAKE_C_COMPILER}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(GGE ${HOME_PATH}/devel/gorgon-3.x)
set(GRAPHICS ${HOME_PATH}/devel/graphics)
set(SOURCES
src/source.cpp
src/algorithms.h
src/cloud-gen.h
src/latex.h
src/macros.h
src/matrix.h
src/utils.h
)
include_directories(${GGE})
include_directories(${GRAPHICS})
add_executable(uwmf ${SOURCES})
target_link_libraries(uwmf ${GGE}/build/libGGE.a)
UPDATE
ereOn's answer did the trick. I also had to install libalut-dev:i386 and link (-lalut) to successfully compile. I get many warning messages like the following (probably due to introducing additional linkage of same library)
/usr/bin/ld: Warning: type of symbol `glDrawBuffers' changed from 2 to 1 in ../devel/gorgon-3.x/build/libGGE.a(OpenGL.cpp.o)
/usr/bin/ld: Warning: type of symbol `glGetAttribLocation' changed from 2 to 1 in ../devel/gorgon-3.x/build/libGGE.a(OpenGL.cpp.o)
but these are not part of this question. Thank you for your help.
It could be that the symbols that are reported missing are not used by your game engine library and were thus "optimized out" to make for a smaller binary.
Try linking your target executable explicitely with -lX11 and -lGL to see if that works.
As #ereOn said, the linker could have optimized out some symbols. Try linking with -Wl,--no-as-needed.
I am trying to include Boost libraries in my project and have been facing issues in the same. I am on Ubuntu 12.10 with Codeblocks IDE and tried installing the libraries manually reading instructions from the site, but was getting error's with header as well as to-be-built-before-use libraries.
I then installed the libraries via terminalby sudo apt-get install libboost-all-dev. After this, in my programs on Codeblocks, I can include headers like #include <boost/regex.hpp> but when I try to include the header for the Filesystem library ( #include "boost/filesystem/operations.hpp" ), I am getting the following error:
/usr/include/boost/system/error_code.hpp|214|undefined reference to boost::system::generic_category()'|
I am not sure how to resolve this error (specifically in Codeblocks on Linux). I really could use some help here.
Compiler : Gcc
Program code: Only tried inlcuding the above file system operations.hpp file.
Build log from Codeblocks:
Build started on: 20-11-2012 at 18:02.53
Build ended on: 20-11-2012 at 18:02.54
-------------- Build: Debug in libopenFrameworks ---------------
Target is up to date.
-------------- Build: Debug in reader1 ---------------
make -s -f Makefile Debug
linking i686 bin/reader1_debug linux
obj/i686Debug/src/testApp.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
obj/i686Debug/src/main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [bin/reader1_debug] Error 1
Process terminated with status 2 (0 minutes, 1 seconds)
6 errors, 0 warnings
You should link in the libboost_system library. I am not sure about codeblocks, but the g++ command-line option on your platform would be
-lboost_system
Depending on the boost version libboost-system comes with the -mt suffix which should indicate the libraries multithreading capability.
So if -lboost_system cannot be found by the linker try -lboost_system-mt.
This answer actually helped when using Boost and cmake.
Adding add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY) for cmake file.
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.12)
project(proj)
set(CMAKE_CXX_STANDARD 17)
set(SHARED_DIR "${CMAKE_SOURCE_DIR}/../shared")
set(BOOST_LATEST_DIR "${SHARED_DIR}/boost_1_68_0")
set(BOOST_LATEST_BIN_DIR "${BOOST_LATEST_DIR}/stage/lib")
set(BOOST_LATEST_INCLUDE_DIR "${BOOST_LATEST_DIR}/boost")
set(BOOST_SYSTEM "${BOOST_LATEST_BIN_DIR}/libboost_system.so")
set(BOOST_FS "${BOOST_LATEST_BIN_DIR}/libboost_filesystem.so")
set(BOOST_THREAD "${BOOST_LATEST_BIN_DIR}/libboost_thread.so")
set(HYRISE_SQL_PARSER_DIR "${SHARED_DIR}/hyrise_sql_parser")
set(HYRISE_SQL_PARSER_BIN_DIR "${HYRISE_SQL_PARSER_DIR}")
set(HYRISE_SQL_PARSER_INCLUDE_DIR "${HYRISE_SQL_PARSER_DIR}/src")
set(HYRISE_SQLPARSER "${HYRISE_SQL_PARSER_BIN_DIR}/libsqlparser.so")
include_directories(${CMAKE_SOURCE_DIR} ${BOOST_LATEST_INCLUDE_DIR} ${HYRISE_SQL_PARSER_INCLUDE_DIR})
set(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu/")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY)
find_package(Boost 1.68.0 REQUIRED COMPONENTS system thread filesystem)
add_executable(proj main.cpp row/row.cpp row/row.h table/table.cpp table/table.h page/page.cpp page/page.h
processor/processor.cpp processor/processor.h engine_instance.cpp engine_instance.h utils.h
meta_command.h terminal/terminal.cpp terminal/terminal.h)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(proj PUBLIC Boost::system Boost::filesystem Boost::thread ${HYRISE_SQLPARSER})
endif()
It's a linker problem. Include the static library path into your project.
For Qt Creator open the project file .pro and add the following line:
LIBS += -L<path for boost libraries in the system> -lboost_system
In my case Ubuntu x86_64:
LIBS += -L/usr/lib/x86_64-linux-gnu -lboost_system
For Codeblocks, open up Settings->Compiler...->Linker settings tab and add:
boost_system
to the Link libraries text widget and press OK button.
I searched for a solution as well, and none of the answers I encountered solved the error, Until I found the answer of "ViRuSTriNiTy" to this thread: Undefined reference to 'boost::system::generic_category()'?
according to that answer, try to add these lines to your cmake file:
find_package(Boost 1.55.0 REQUIRED COMPONENTS system filesystem)
include_directories(... ${Boost_INCLUDE_DIRS})
link_directories(... ${Boost_LIBRARY_DIRS})
target_link_libraries(... ${Boost_LIBRARIES})
Same problem on building a simple boost example, solved after i changed the g++ compiler flag from -std=c++14 to -std=c++11.
And I noticed that it's a C++11 Example...
I had the same problem and also use Linux Mint (as nuduoz) . I my case problem was solved after i added boost_system to GCC C++ Linker->Libraries.
You could come across another problem. After installing Boost on the Linux Mint I've had the same problem. Linking -lboost_system or -lboost_system-mt haven't worked because library have had name libboost_system.so.1.54.0.
So the solution is to create symbolic link to the original file. In my case
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0 /usr/lib/libboost_system.so
For more information see this question.
g++ -lboost_system -lboost_filesystem userentry.cpp -o userentry
worked perfectly under debian. (boost c++ libraries installed with apt-get).
Il the library is not installed you should give boost libraries folder:
example:
g++ -L/usr/lib/x86_64-linux-gnu -lboost_system -lboost_filesystem prog.cpp -o prog
try
g++ -c main.cpp && g++ main.o /usr/lib/x86_64-linux-gnu/libboost_system.so && ./a.out
/usr/lib/x86_64-linux-gnu/ is the location of the boost library
use find /usr/ -name '*boost*.so' to find the boost library location
In my project I prefer to use header-only libraries. So nothing above was helpful. What did really help is:
add_definitions(-DBOOST_SYSTEM_NO_DEPRECATED)
After testing the proposed solutions described above, I found only these few of lines would work.
I am using Ubuntu 16.04.
cmake_minimum_required(VERSION 3.13)
project(myProject)
set(CMAKE_CXX_STANDARD 11)
add_executable(myProject main.cpp)
find_package(Boost 1.58.0 REQUIRED COMPONENTS system filesystem)
target_link_libraries(myProject ${Boost_LIBRARIES})
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:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -lpthread -DNDEBUG -DEIGEN_MPL2_ONLY")
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Wall -lpthread -DEIGEN_MPL2_ONLY")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -lpthread -I/usr/include/freetype2 -DNDEBUG -DEIGEN_MPL2_ONLY")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -lpthread -I/usr/include/freetype2 -DEIGEN_MPL2_ONLY")
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:
$ find /lib -name "*pthread*"
/lib/x86_64-linux-gnu/libpthread-2.19.so
/lib/x86_64-linux-gnu/libpthread.so.0
The pthread_create is also found:
$ nm /lib/x86_64-linux-gnu/libpthread.so.0 | grep "pthread_create"
0000000000008430 t __pthread_create_2_1
00000000000081430 T pthread_create##GLIBC_2.2.5
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/:
if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
# We have sproc
set(CMAKE_USE_SPROC_INIT 1)
else()
# Do we have pthreads?
CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
if(CMAKE_HAVE_PTHREAD_H)
#
# We have pthread.h
# Let's check for the library now.
#
set(CMAKE_HAVE_THREADS_LIBRARY)
if(NOT THREADS_HAVE_PTHREAD_ARG)
# Check if pthread functions are in normal C library
CHECK_SYMBOL_EXISTS(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
if(CMAKE_HAVE_LIBC_CREATE)
set(CMAKE_THREAD_LIBS_INIT "")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
endif()
if(NOT CMAKE_HAVE_THREADS_LIBRARY)
# Do we have -lpthreads
CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
if(CMAKE_HAVE_PTHREADS_CREATE)
set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
endif()
# Ok, how about -lpthread
CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
if(CMAKE_HAVE_PTHREAD_CREATE)
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
endif()
if(CMAKE_SYSTEM MATCHES "SunOS.*")
# On sun also check for -lthread
CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
if(CMAKE_HAVE_THR_CREATE)
set(CMAKE_THREAD_LIBS_INIT "-lthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(Threads_FOUND TRUE)
endif()
endif()
endif()
endif()
if(NOT CMAKE_HAVE_THREADS_LIBRARY)
# If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
if("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
message(STATUS "Check if compiler accepts -pthread")
try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
${CMAKE_BINARY_DIR}
${CMAKE_ROOT}/Modules/CheckForPthreads.c
CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
COMPILE_OUTPUT_VARIABLE OUTPUT)
if(THREADS_HAVE_PTHREAD_ARG)
if(THREADS_PTHREAD_ARG STREQUAL "2")
set(Threads_FOUND TRUE)
message(STATUS "Check if compiler accepts -pthread - yes")
else()
message(STATUS "Check if compiler accepts -pthread - no")
file(APPEND
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
endif()
else()
message(STATUS "Check if compiler accepts -pthread - no")
file(APPEND
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
endif()
endif()
if(THREADS_HAVE_PTHREAD_ARG)
set(Threads_FOUND TRUE)
set(CMAKE_THREAD_LIBS_INIT "-pthread")
endif()
endif()
endif()
endif()
Edit 3
Used a minimal Cmakelists.txt as follows:
cmake_minimum_required (VERSION 2.4)
find_package(Threads)
Which produced the following output:
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found.
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
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.
at an Ubuntu 18.04.1 LTS this installation gave me all the files needed:
apt -y install libboost-tools-dev libboost-thread1.62-dev magics++
/usr/lib/x86_64-linux-gnu/libpthread.a
/usr/lib/x86_64-linux-gnu/libpthread.so
/usr/lib/x86_64-linux-gnu/libpthread_nonshared.a
no more errors "/usr/bin/ld: cannot find -lpthreads" after
Edit1:
All references below is for Ubuntu.
Package named libpthread-stubs0 is likely only a stub, so won't have the pthread_create function.
Do you have this?
$ find /lib -name "*pthread*"
/lib/x86_64-linux-gnu/libpthread-2.15.so
/lib/x86_64-linux-gnu/libpthread.so.0
Check for the symbol pthread_create which should exist.
$ nm /lib/x86_64-linux-gnu/libpthread.so.0 | grep "pthread_create"
0000000000008140 t __pthread_create_2_1
0000000000008140 T pthread_create##GLIBC_2.2.5
If that doesn't work, you may need the dev version of pthread which is in libc6-dev. You can search for the package contents which has libpthread.so in http://packages.ubuntu.com/.
Note: Also, it's failing on -lpthreads. Should it be -lpthread instead (without the s)?
Edit 2
Create a simple CMakeLists.txt with the following and run cmake.
cmake_minimum_required (VERSION 2.8.7)
find_package(Threads)
What's the output? Does it find pthread?
My output is:
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found.
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
This appears to be a long-standing CMake bug. Something else is going wrong, CMake gets confused, and reports this spurious problem instead of the real error.
Look for "thread" in your CMakeLists.txt file and temporarily remove that.
In my case, this immediately pinpointed a library (or rather, its development package) that was missing. Installed it, added it to debian/control's Build-Depends: section, recompiled, everything worked.
on ubuntu 18.04 I solved as below.
$ sudo apt-get install libboost-all-dev
I had the exact same problem, with the minimal Cmakelists.txt giving me the same output.
To solve this, just upgrade cmake to the lastest version (3.15 in my case)
I found out what was causing my issue. I initially did it with cmake2, but the project needed cmake3. I changed it to cmake3, but it didn't do a clean build, so some leftover garbage was messing everything up. When I cleaned everything and used cmake3 it worked.
I also meet this issue. Exactly the same situation: have pthread lib under /lib/x86..., but the find_package() always gives a "can not find lpthread error".
And after some check and consulation to my friend, we find that in my case, I build the cmake from source code and let the cmake link search path be wrong. So we deinstall the self-buid version and reinstall the cmake in a "correct" way by adding the apt source and using apt get install. That solves my issue. Hope this could help guys in the same situation.
Kindly try to install one dependency glibc-static
On Ubuntu you can try apt-get install build-essential
On other linux you may install package similar to glibc-static.
This question has many answers; it seems that many obscure issues can cause this bug. The other answers didn't work for me, and though my Output/Error logs were fairly clean, I couldn't find any additional error messages or failures there. So I'll add what I did in case it helps anyone who is in my situation.
In short, my problem was fixed when I deleted the entire build directory and rebuilt from scratch. Deleting only the CMakeCache wasn't enough. If you have this issue, try doing that and see if it works