2

I've installed Boost using this command

sudo apt-get install libboost-all-dev

and I wrote this simple example in main.cpp

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
  boost::asio::io_service io;

  boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
  t.wait();

  std::cout << "Hello, world!" << std::endl;

  return 0;
}

And in my CMakeLists.txt I have this:

cmake_minimum_required(VERSION 2.8)

find_package(Boost REQUIRED)
if(NOT Boost_FOUND)
    message(SEND_ERROR "Failed to find Boost")
    return()
else()
    include_directories(${Boost_INCLUDE_DIR})
endif()

add_executable(main main.cpp)

CMake worked correctly, but after launching with make I got a few errors:

main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'

How to correctly include boost in my CMakeLists.txt so that cmake will find libraries ?

Farhad
  • 4,119
  • 8
  • 43
  • 66
Dsdsd
  • 43
  • 1
  • 5

3 Answers3

3

You need to link against the boost libraries. FindBoost provides the variable Boost_LIBRARIES for this:

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

For more information, see the FindBoost documentation. There's an example near the end.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
0
main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'

It's failing at the link step. You're not linking to the system library. You need to do that.

You're not running into any error with regard to CMake making use of boost. You just need to tell it that system needs to be linked in.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
0

To add on to the previous answers, here is the list of Boost librairies you need to link. (as of Boost 1.65)

All other boost libraires can be used by simply including the header.

Khoyo
  • 1,253
  • 11
  • 20