2
votes

This is my CMakeLists.txt:

set(CMAKE_VERBOSE_MAKEFILE ON)
cmake_minimum_required(VERSION 3.8)
project(spider)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(spider ${SOURCE_FILES})
target_link_libraries(spider ${Boost_LIBRARIES})
target_link_libraries(spider Threads::Threads)
target_link_libraries(spider
        ${Boost_PROGRAM_OPTIONS_LIBRARIES}
        ${Boost_FILESYSTEM_LIBRARIES}
        ${Boost_SYSTEM_LIBRARIES}
        )
SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -lboost_system -DBOOST_SYSTEM_NO_DEPRECATED" )

This are my includes files from main.cpp:

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

using boost::asio::ip::udp;

I tried downloading and building boost, it causes tons of trobules, so I decided to use boost which comes with cygwin installer. I do have boost_system and boost in my /include folder, so I don't have to setup boost in my Cmakelists.txt:

enter image description here

The question is what to do with the error:

/usr/include/boost/system/error_code.hpp:322: undefined reference to `boost::system::system_category()'

As you can see, I've tried -lboost_system and -DBOOST_SYSTEM_NO_DEPRECATED, the flags are passed to g++:

/usr/bin/c++.exe -lboost_system -DBOOST_SYSTEM_NO_DEPRECATED -g -Wl,--enable-auto-import CMakeFiles/spider.dir/main.cpp.o -o spider.exe -Wl,--out-implib,libspider.dll.a -Wl,--major-image-version,0,--minor-image-version,0

but this doesn't help. Any ideas ?

2
The order of your link is wrong. The -lboost_system has to come after your object files. I don't know how to adjust your CMakefile to fix the issue however. - Dave S
Don't add the target_link_libraries manually, use the variables provided by FindBoost.cmake. - usr1234567

2 Answers

0
votes

adding

target_link_libraries(spider boost_system) 

instead of SET CMAKE_CXX_FLAGS to the end of cmake file fixes the problem, rootcause is that the flag should be in the end, as commenters noted.

0
votes

My solution was to put the library in directly instead of -lboost_system.

LIBS= -lboost_thread /usr/local/lib/libboost_system.so.1.68.0

Not sure why boost_system doesn't resolve correctly in < 1.70 (as I didn't have this error with that version of the libraries, using old version to test other issues)

Hope this helps.