Boost logo

Boost Users :

From: Russell Haley (rhaley_at_[hidden])
Date: 2025-02-11 17:44:17


Hello,

I am implementing a serial port protocol in a Linux based library. We build boost ourselves (1.86.0) for inclusion in thrift so I am trying to use as much as I can just from the Boost libraries to gain some experience with them.

My serial port driver is using the serial_port class from Boost ASIO. It's super cool and working well. To include Boost ASIO in my CMake target, all I have done is this:

cmake_minimum_required(VERSION 3.30)
set(CMAKE_CXX_STANDARD 20)
set(BOOST_ROOT "${PROJECT_SOURCE_DIR}/../dataman_service/boost-install-x86_64-Debug/")
find_package(Boost CONFIG)
#This is an alternate use?
#find_package(Boost NO_MODULE)

add_library(L2PUtility
            L2Packet.cpp
            L1Transport.cpp
            crc1021.cpp)
target_include_directories(L2PUtility PUBLIC ${CMAKE_SOURCE_DIR} ${BOOST_ROOT}/include)

Note there is no target_link_libraries, but ASIO works. I thought that was because it's an HPP/Header only implementation?

Since I need to add some threading to the project, I thought I would use the boost_thread library. Examples online show the inclusion of boost/thread.hpp, so I assumed it is another header only implementation and no link library is needed but I am getting a linking error:

[2/4] Linking CXX executable L2PSecondary
FAILED: L2PSecondary
: && /usr/bin/c++ -g CMakeFiles/L2PSecondary.dir/main.cpp.o -o L2PSecondary libL2PUtility.a && :
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): warning: relocation against `_ZTVN5boost6detail16thread_data_baseE' in read-only section `.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): in function `L1Transport::start_detached()':
/home/rhaley/tuleap/BoostAsioL2p/L1Transport.cpp:440:(.text+0x1b32): undefined reference to `boost::thread::detach()'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): in function `boost::detail::thread_data_base::thread_data_base()':
/home/rhaley/tuleap/BoostAsioL2p/../stroma_dataman_service/boost-install-x86_64-Debug/include/boost/thread/pthread/thread_data.hpp:162:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): in function `boost::thread::start_thread()':
/home/rhaley/tuleap/BoostAsioL2p/../stroma_dataman_service/boost-install-x86_64-Debug/include/boost/thread/detail/thread.hpp:182:(.text._ZN5boost6thread12start_threadEv[_ZN5boost6thread12start_threadEv]+0x28): undefined reference to `boost::thread::start_thread_noexcept()'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): in function `boost::thread::~thread()':
/home/rhaley/tuleap/BoostAsioL2p/../stroma_dataman_service/boost-install-x86_64-Debug/include/boost/thread/detail/thread.hpp:257:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x18): undefined reference to `boost::thread::detach()'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): in function `boost::thread::get_id() const':
/home/rhaley/tuleap/BoostAsioL2p/../stroma_dataman_service/boost-install-x86_64-Debug/include/boost/thread/detail/thread.hpp:738:(.text._ZNK5boost6thread6get_idEv[_ZNK5boost6thread6get_idEv]+0x27): undefined reference to `boost::thread::native_handle()'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): in function `boost::thread::join()':
/home/rhaley/tuleap/BoostAsioL2p/../stroma_dataman_service/boost-install-x86_64-Debug/include/boost/thread/detail/thread.hpp:762:(.text._ZN5boost6thread4joinEv[_ZN5boost6thread4joinEv]+0x90): undefined reference to `boost::thread::join_noexcept()'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o): in function `boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf<void (L1Transport::*)(), void, L1Transport>, boost::_bi::list<boost::_bi::value<L1Transport*> > > >::~thread_data()':
/home/rhaley/tuleap/BoostAsioL2p/../stroma_dataman_service/boost-install-x86_64-Debug/include/boost/thread/detail/thread.hpp:94:(.text._ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi2mfIM11L1TransportFvvEvS6_JEEENS2_4listIJNS2_5valueIPS6_EEEEEEEED2Ev[_ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi2mfIM11L1TransportFvvEvS6_JEEENS2_4listIJNS2_5valueIPS6_EEEEEEEED5Ev]+0x26): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/usr/bin/ld: libL2PUtility.a(L1Transport.cpp.o):(.data.rel.ro._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi2mfIM11L1TransportFvvEvS6_JEEENS2_4listIJNS2_5valueIPS6_EEEEEEEEE[_ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi2mfIM11L1TransportFvvEvS6_JEEENS2_4listIJNS2_5valueIPS6_EEEEEEEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

I tried adding a target_link_library:

target_link_libraries(L2PUtility PUBLIC boost_thread)

To no avail. I now get a missing library in the EXECUTABLE, not in the library that actually includes boost_thread! Does that mean it found it for the L2Utility library? (so confused...)

Here is my executable target:


add_executable(L2PSecondary
        main.cpp)
target_include_directories(L2PSecondary PRIVATE ${CMAKE_SOURCE_DIR} ${BOOST_ROOT}/include)
target_link_libraries(L2PSecondary PRIVATE L2PUtility)

And here is the linking error:

[1/2] Linking CXX executable L2PSecondary
FAILED: L2PSecondary
: && /usr/bin/c++ -g CMakeFiles/L2PSecondary.dir/main.cpp.o -o L2PSecondary libL2PUtility.a -lboost_thread && :
/usr/bin/ld: cannot find -lboost_thread: No such file or directory
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I confirmed that my boost installation has the library libboost_tread.a.

rhaley_at_linuxdev2:~/tuleap/dataman_service/boost-install-x86_64-Debug$ find . -iname "*thread*.a"
./lib/libboost_thread.a

Adding link libraries to the executable targets doesn't fix anything. Can anyone point out what I am doing wrong?

Thanks in advance,
Russ



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net