Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build errors while trying to add boost::stacktrace to CMake project

I'm trying to add boost::stacktrace library to my project via CMake. CMake finds all required libs well, but when I call boost::stacktrace::stacktrace() to print stack information into std::cout - it gives me the following error:

undefined reference to 'dladdr'

I've already tried to add the -ldl compile flag to the CMAKE_CXX_FLAGS compile flags to see if it can help, but it doesn't work. I understand that I missed some required packages, but I don't know which. Maybe you could advise me.

  • Gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04)
  • Boost version 1.68.0

Here is some code:

CMakeLists.txt

#-------------------------------------------------------------------#
# cmake version & project name

cmake_minimum_required(VERSION 3.10)
project(stack_trace_exmpl)

#-------------------------------------------------------------------#
# project flags config

set(CMAKE_CXX_STANDARD      17)
set(CMAKE_C_STANDARD        11)

set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG} -g3 -pthread")
set(CMAKE_C_FLAGS_DEBUG     "-g3")
set(CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS} -Wall -pthread -ldl")
set(CMAKE_C_FLAGS           "-Wall")

set(BOOST_ROOT              "/opt/Boost_1_68_0/")

#-------------------------------------------------------------------#
# find Boost libs

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package(
    Boost 1.68.0
    COMPONENTS REQUIRED
        stacktrace_basic
        stacktrace_backtrace
        stacktrace_addr2line
        stacktrace_noop
)

#-------------------------------------------------------------------#
# project sources & project's type

file(GLOB SRCS "*.cpp")
file(GLOB HDRS "*.hpp")

add_executable(${PROJECT_NAME} ${HDRS} ${SRCS})

#-------------------------------------------------------------------#
# link libraries

target_link_libraries(${PROJECT_NAME}
    PUBLIC ${Boost_STACKTRACE_BASIC_LIBRARY}
    PUBLIC ${Boost_STACKTRACE_BACKTRACE_LIBRARY}
    PUBLIC ${Boost_STACKTRACE_ADDR2LINE_LIBRARY}
    PUBLIC ${Boost_STACKTRACE_NOOP_LIBRARY}
)

#-------------------------------------------------------------------#
# include directories

target_include_directories(${PROJECT_NAME}
    PUBLIC ${Boost_INCLUDE_DIRS}
)

#-------------------------------------------------------------------#

main.cpp

#include <iostream>

#include <boost/stacktrace.hpp>

void tracesToCout( int _n )
{
    std::cout << boost::stacktrace::stacktrace();

    if( _n > 0 )
    {
        tracesToCout( --_n );
    }
}

int main()
{
    tracesToCout( 5 );

    return 0;
}

CMake output:

-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /opt/Boost_1_68_0/include (found suitable version "1.68.0", minimum required is "1.68.0") found components:  stacktrace_basic stacktrace_backtrace stacktrace_addr2line stacktrace_noop 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Work/boost_stacktrace/Debug64

build output:

CMakeFiles/stack_trace_exmpl.dir/main.cpp.o: In function `boost::stacktrace::detail::location_from_symbol::location_from_symbol(void const*)':
main.cpp:(.text._ZN5boost10stacktrace6detail20location_from_symbolC2EPKv[_ZN5boost10stacktrace6detail20location_from_symbolC5EPKv]+0x4e): undefined reference to `dladdr'
CMakeFiles/stack_trace_exmpl.dir/main.cpp.o: In function `boost::stacktrace::frame::name[abi:cxx11]() const':
main.cpp:(.text._ZNK5boost10stacktrace5frame4nameB5cxx11Ev[_ZNK5boost10stacktrace5frame4nameB5cxx11Ev]+0x31): undefined reference to `dladdr'
collect2: error: ld returned 1 exit status
CMakeFiles/stack_trace_exmpl.dir/build.make:87: recipe for target 'stack_trace_exmpl' failed
make[2]: *** [stack_trace_exmpl] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/stack_trace_exmpl.dir/all' failed
make[1]: *** [CMakeFiles/stack_trace_exmpl.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
like image 605
Yevhenii Mamontov Avatar asked Oct 24 '25 15:10

Yevhenii Mamontov


1 Answers

Turning my comment into an answer:

Because the dl library is needed by boost::stacktrace (see the Boost requirements), the dl library should be linked after the Boost stacktrace libraries are linked. To ensure this ordering, link the dl library (and perhaps pthread as well) at the end of the target_link_libraries() call instead:

target_link_libraries(${PROJECT_NAME}
    PUBLIC ${Boost_STACKTRACE_BASIC_LIBRARY}
    PUBLIC ${Boost_STACKTRACE_BACKTRACE_LIBRARY}
    PUBLIC ${Boost_STACKTRACE_ADDR2LINE_LIBRARY}
    PUBLIC ${Boost_STACKTRACE_NOOP_LIBRARY}
    PUBLIC pthread dl
)
like image 79
Kevin Avatar answered Oct 26 '25 06:10

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!