0

I am trying to compile the Boost ASIO examples using CMake on Ubuntu 13.04 with GCC 4.73.

http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp11/echo/blocking_tcp_echo_client.cpp

http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp

With the following CMake File:

cmake_minimum_required(VERSION 2.8)

ADD_DEFINITIONS(-std=c++11)
project(server)

find_package( Boost 1.53.0 REQUIRED system)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

add_executable( server server.cpp )
target_link_libraries(server ${Boost_LIBRARIES} )

add_executable( client client.cpp )
target_link_libraries(client ${Boost_LIBRARIES})

The project should be using C++11 and be platform independent. When compiling using the CMakefile I get something like

undefined reference to `pthread_join'

My colleague can compile exactly the same Makefile without any problems.

The question: Is there any way to force CMake or Boost not to use PThreads but the C++11 Threads to stay platform independent?

EDIT: The server compiles without problems while only the client has problems.

Till
  • 682
  • 1
  • 6
  • 19
  • 5
    Correct me if I'm wrong, but I'm pretty sure C++11 doesn't have its own thread implementation. It's a wrapper around pthread or whatever is appropriate to the platform. I'm sure Boost handles the portability issues. – Fred Larson Sep 11 '13 at 17:42
  • @FredLarson is correct, C++11 _can't_ have it's own thread implementation, at some point is has to rely on the threading model that the underlying operating system provides, in the case of Linux, that is `pthread`. – Chad Sep 11 '13 at 17:47
  • Okay, thanks. So you have any clue, why it works at my colleagues Linux machine with the same make file? Maybe his compiler automatically links pthread? – Till Sep 11 '13 at 17:54
  • If it's the same compiler, with the same arguments, then it should be the same. So, the first thing I would do is to verify that you're both invoking the compiler with the same arguments. There is a way to get cmake to output the commands it is executing. I would use that and compare. – Dave S Sep 11 '13 at 18:13
  • You'll need to give the linker a `-pthread` argument. (I don't know how CMake works, but I'm sure there must be a simple way to do that). – Mike Seymour Sep 11 '13 at 18:17
  • 1
    You may use find_package(Threads REQUIRED) as shown here: http://stackoverflow.com/questions/5395309/cmake-and-threads – Alexey Sep 13 '13 at 05:58

1 Answers1

5

Is there any way to force CMake or Boost not to use PThreads but the C++11 Threads to stay platform independent?

No, both Boost.Threads and GCC's C++ threads library are implemented as a fairly thin layer over Pthreads.

So you have any clue, why it works at my colleagues Linux machine with the same make file? Maybe his compiler automatically links pthread?

It's possible to configure GCC with --enable-libstdcxx-time=rt which causes the C++ standard library to be linked to librt and libpthread. That means you don't need to link with -pthread, so it's possible your colleague is using GCC configured like that. That configuration option is not the default because automatically linking to libpthread.so negatively affects performance of single-threaded programs that use some parts of the C++ standard library.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521