Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let cmake use "-pthread" instead of -lpthread"?

Here is my environment:

  • OS: Ubuntu 14.10
  • gcc: 4.9
  • cmake: 2.8, 3.1(both tried)
  • project: muduo

Recently, I've started to learn network programming and download muduo for learning. While I have problems to build the source, because cmake will complain with "cannot find -lpthreads".

I've done some research. It is mostly caused by the newer version of gcc under Ubuntu 14.10. The gcc-4.9 will use "-pthread" to link to pthread library, however, the older version of gcc uses "-lpthreads". It seems that cmake still uses "-lpthreads" and I don't know how to correct this...

Below is the error log:

  File /home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>

int main(int argc, char** argv)
{
  (void)argv;
#ifndef pthread_create
  return ((int*)(&pthread_create))[argc];
#else
  (void)argc;
  return 0;
#endif
}

Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/make" "cmTryCompileExec2265723491/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec2265723491.dir/build.make CMakeFiles/cmTryCompileExec2265723491.dir/build
make[1]: Entering directory '/home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp'
/usr/local/bin/cmake -E cmake_progress_report /home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec2265723491.dir/CheckFunctionExists.c.o
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTryCompileExec2265723491.dir/CheckFunctionExists.c.o   -c /usr/local/share/cmake-3.1/Modules/CheckFunctionExists.c
Linking C executable cmTryCompileExec2265723491
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec2265723491.dir/link.txt --verbose=1
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create   CMakeFiles/cmTryCompileExec2265723491.dir/CheckFunctionExists.c.o  -o cmTryCompileExec2265723491 -rdynamic -lpthreads 
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
CMakeFiles/cmTryCompileExec2265723491.dir/build.make:88: recipe for target 'cmTryCompileExec2265723491' failed
make[1]: Leaving directory '/home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp'
Makefile:118: recipe for target 'cmTryCompileExec2265723491/fast' failed
make[1]: *** [cmTryCompileExec2265723491] Error 1
make: *** [cmTryCompileExec2265723491/fast] Error 2

Anybody knows how to fix this and let me compile muduo on Ubuntu 14.10?

like image 585
Jack Sun Avatar asked Jan 19 '15 16:01

Jack Sun


2 Answers

Set the compile or link flags of the target:

set_target_properties(target1 PROPERTIES COMPILE_FLAGS -pthread LINK_FLAGS -pthread)

Or set global variables:

set(CMAKE_LINKER_FLAGS "-pthread" CACHE STRING "Linker Flags" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS    "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
like image 187
maxint Avatar answered Nov 07 '22 07:11

maxint


I've just received answer from the author of muduo. It is caused by the lack of libboost-dev. The error message is misleading.

After apply the following command:

sudo apt-get install g++ libboost-dev cmake make git

The build will succeed.

like image 2
Jack Sun Avatar answered Nov 07 '22 08:11

Jack Sun