4

I'm trying to compile a boost tutorial example from http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tuttimer1.html.

My CMakeLists.txt looks like the following:

project(boost)
add_executable(timer1 timer1.cpp)
set_target_properties(timer1 PROPERTIES LINK_FLAGS -lboost_system,-lpthread)

trying to build the whole thing with cmake, I get:

/var/www/C++/boost/build$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /var/www/C++/boost/build
Scanning dependencies of target timer1
[100%] Building CXX object CMakeFiles/timer1.dir/timer1.cpp.o                                                                                                    
Linking CXX executable timer1                                                                                                                                    
/usr/bin/ld: cannot find -lboost_system,-lpthread                                                                                                                
collect2: ld returned 1 exit status
make[2]: *** [timer1] Błąd 1
make[1]: *** [CMakeFiles/timer1.dir/all] Błąd 2
make: *** [all] Błąd 2

But when I run:

g++ timer1.cpp -lboost_system -lpthread -o timer1

manually, everything works fine. Can someone please point me on what do I do wrong?

PS When I try to use solution described in Turning on linker flags with CMake, I add to cmake the following lines:

set(CMAKE_SHARED_LINKER_FLAGS "-lboost_system,-lpthread")
set(CMAKE_MODULE_LINKER_FLAGS "-lboost_system,-lpthread")
set(CMAKE_EXE_LINKER_FLAGS "-lboost_system,-lpthread")

and I get the same error as above.

Community
  • 1
  • 1
ducin
  • 25,621
  • 41
  • 157
  • 256

2 Answers2

5

I strongly suggest that you use the CMake integrated FindPackage. CMake will find boost and pthreads for you.

Your CMakeLists.txt should look like this:

find_package( Boost COMPONENTS thread system filesystem REQUIRED ) #whatever libs you need
include_directories( ${Boost_INCLUDE_DIRS} )
find_package( Threads )

In the subfolder src:

set( LIBS_TO_LINK
    ${Boost_LIBRARIES}
    ${CMAKE_THREAD_LIBS_INIT}
)

target_link_libraries( myApp
    ${LIBS_TO_LINK}
)
Kirell
  • 9,228
  • 4
  • 46
  • 61
2

/usr/bin/ld: cannot find -lboost_system,-lpthread

Here the linker is looking for a library libboost_system,-lpthread.so. It is exceedingly unlikely that such a library exists on any UNIX system.

You probably want:

set(CMAKE_EXE_LINKER_FLAGS "-lboost_system -lpthread")
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Now I'm getting: Linking CXX executable server CMakeFiles/server.dir/server.cpp.o: In function `__static_initialization_and_destruction_0(int, int)': server.cpp:(.text+0x810): undefined reference to `boost::system::generic_category()' [...] collect2: ld returned 1 exit status make[2]: *** [server] Error 1 make[1]: *** [CMakeFiles/server.dir/all] Error 2 make: *** [all] Error 2 – ducin Dec 18 '12 at 09:21
  • 1
    @tkoomzaaskz This error usually means that you are linking `C++` executable with plain-`C` (`gcc`) compiler. I don't know how to tell CMake that it should use `g++` instead of `gcc`. One possible workaround is to add `-lstdc++` to the list of `LINKER_FLAGS`, but I suggest only using that if you can't figure out the proper `g++` solution. – Employed Russian Dec 18 '12 at 17:28