5

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?

Jack Sun
  • 95
  • 1
  • 1
  • 5
  • 2
    Do you use `find_package(Threads REQUIRED)` in your `CMakeLists.txt` or what? You should show your file.. – stefan Jan 19 '15 at 16:30
  • Check here, if it helps http://stackoverflow.com/questions/23250863/difference-between-pthread-and-lpthread-while-compiling – DumbCoder Jan 19 '15 at 16:43
  • I've just received answer from the author of muduo. It is because of the lack of libboost-dev. The error message is misleading... – Jack Sun Jan 20 '15 at 07:27

3 Answers3

4

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)
maxint
  • 1,005
  • 8
  • 12
2

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.

hardillb
  • 54,545
  • 11
  • 67
  • 105
Jack Sun
  • 95
  • 1
  • 1
  • 5
  • 1
    I have all these packages installed, the problem is not related to installed packages. – Avio Jul 04 '16 at 12:22
1

For CMake 3.1 or newer version, use THREADS_PREFER_PTHREAD_FLAG to prefer -pthread, for example,

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
guest
  • 11
  • 1