0

I want to use boost.asio header in my project, but when I include it's .hpp file I got this output error on compile:

  • I need boost.asio for crow to route my web request.

    /home/john/Downloads/clion-1.2.4/bin/cmake/bin/cmake --build /home/john/.CLion12/system/cmake/generated/a3f08900/a3f08900/Release --target rcp -- -j 8 [ 50%] Linking CXX executable /home/john/projects/rightChoiceProperty/bin/rcp CMakeFiles/rcp.dir/main.cpp.o: In function _GLOBAL__sub_I_main': main.cpp:(.text.startup+0x53): undefined reference toboost::system::generic_category()' main.cpp:(.text.startup+0x58): undefined reference to boost::system::generic_category()' main.cpp:(.text.startup+0x5d): undefined reference toboost::system::system_category()' main.cpp:(.text.startup+0x62): undefined reference to `boost::system::system_category()' collect2: error: ld returned 1 exit status CMakeFiles/rcp.dir/build.make:94: recipe for target '/home/john/projects/rightChoiceProperty/bin/rcp' failed make[3]: * [/home/john/projects/rightChoiceProperty/bin/rcp] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/rcp.dir/all' failed make[2]: [CMakeFiles/rcp.dir/all] Error 2 CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/rcp.dir/rule' failed make[1]: [CMakeFiles/rcp.dir/rule] Error 2 Makefile:118: recipe for target 'rcp' failed make: * [rcp] Error 2

I'm using CLion 1.2.4 as IDE this is my main.cpp content:

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

using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

And this is my CMakeLists.txt file content:

cmake_minimum_required(VERSION 3.3)
project(rcp)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lboost_system")

set(SOURCE_FILES main.cpp)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")

include_directories("libraries/boost")

add_executable(rcp ${SOURCE_FILES})

Thank you so much

John Kangari
  • 31
  • 3
  • 10

1 Answers1

2

The -l option is not a compiler option, it's a linker options, so you're setting it for the wrong variable as CMAKE_CXX_FLAGS are only for the compiler.

Instead use e.g. target_link_libraries to add libraries. Like

target_link_libraries(rcp boost_system)

But what you really should do is to find the system-installed Boost libraries and use those. You do that with find_package:

find_package(Boost
    REQUIRED COMPONENTS asio system)

include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(rcp ${Boost_LIBRARIES})
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621