0

So i was just messing around boost.asio. They have a pretty nice tutorial page, where i just followed the instructions and pretty much copied the code. Previous examples are working just fine, but when it comes to threads i get bunch of errors

FAILED: untitled 
: && /usr/bin/c++ -lboost_system -g  CMakeFiles/untitled.dir/main.cpp.o -o untitled   && :
/usr/bin/ld: CMakeFiles/untitled.dir/main.cpp.o: warning: relocation against `_ZTVN5boost6detail16thread_data_baseE' in read-only section `.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]'
/usr/bin/ld: CMakeFiles/untitled.dir/main.cpp.o: in function `boost::detail::thread_data_base::thread_data_base()':
/home/user_me/Sys_Builds/boost_1_79_0/boost/thread/pthread/thread_data.hpp:162: undefined reference to `vtable for boost::detail::thread_data_base'
/usr/bin/ld: CMakeFiles/untitled.dir/main.cpp.o: in function `boost::thread::start_thread()':
/home/user_me/Sys_Builds/boost_1_79_0/boost/thread/detail/thread.hpp:182: undefined reference to `boost::thread::start_thread_noexcept()'
/usr/bin/ld: CMakeFiles/untitled.dir/main.cpp.o: in function `boost::thread::~thread()':
.
.
.

I use the boost 1-79-0. I downloaded binaries from official website and run basically two commands inside of the directory

./bootstrap.sh
./b2 install

After this i just added the line

 include_directories("/home/user_me/Sys_Builds/boost_1_79_0")

to cmake (i use clion as IDE). As said before, it seems like everything is working just fine but threads (which is pretty weird). In case you're just waiting to markup this post as duplicate - i've came across 4 or 5 similarish posts on varius sites and nothing has worked for me.

Solution

As you can read in the comments i was including only header files hence problem with linking the library. Using the link (to post where i should've found the solution before making another) provided #btdrescherjm i rewrote the CMakeLists.txt to
#Project details
cmake_minimum_required(VERSION 3.22)
project(untitled)

#Setting cmake standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#Boost config section
find_package(Boost COMPONENTS thread REQUIRED)

#Adding exec
add_executable(
        ${PROJECT_NAME}
        main.cpp
)

target_link_libraries(
        ${PROJECT_NAME}
        Boost::thread
)
  • `include_directories("/home/user_me/Sys_Builds/boost_1_79_0")` is not correct. You should use find_package() and target_link_libraries() – drescherjm Jul 05 '22 at 17:23
  • Just setting the include directory will work with header only libraries in boost but not libraries that need to be linked. – drescherjm Jul 05 '22 at 17:26
  • How should i use target_link libraries()? I've tried find_package tho "find_package(Boost 1.79.0 COMPONENTS filesystem system REQUIRED)" or "find_package(Boost COMPONENTS filesystem system REQUIRED)" is what i tried – Piotrek Wójtowicz Jul 05 '22 at 17:27
  • Your CMakeLists.txt should look like this: [https://stackoverflow.com/a/41398161/487892](https://stackoverflow.com/a/41398161/487892) – drescherjm Jul 05 '22 at 17:34
  • Thank you all so much it works just fine now. I'm just too much of an idiot and didn't even notice this is linking error. – Piotrek Wójtowicz Jul 05 '22 at 17:42
  • 2
    Use the package configuration script that comes with the boost installation: `set(Boost_DIR "/path/too/boost/install/root") find_package(Boost REQUIRED COMPONENTS thread CONFIG) target_link_libraries(... PRIVATE Boost::thread)` Setting `Boost_DIR` is only required for non-default install locations – fabian Jul 05 '22 at 17:42

0 Answers0