【问题标题】:Build errors while trying to add boost::stacktrace to CMake project尝试将 boost::stacktrace 添加到 CMake 项目时生成错误
【发布时间】:2020-05-19 05:55:24
【问题描述】:

我正在尝试通过 CMake 将 boost::stacktrace 库添加到我的项目中。 CMake 可以很好地找到所有必需的库,但是当我调用 boost::stacktrace::stacktrace() 将堆栈信息打印到 std::cout 时 - 它给了我以下错误:

undefined reference to 'dladdr'

我已经尝试将-ldl 编译标志添加到CMAKE_CXX_FLAGS 编译标志以查看它是否有帮助,但它不起作用。我知道我错过了一些必需的软件包,但我不知道是哪个。也许你可以给我建议。

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

这里有一些代码:

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}
)

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

ma​​in.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 输出:

-- 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

构建输出:

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

【问题讨论】:

  • 您可以尝试在target_link_libraries() 调用的结束 处链接dl 库:target_link_libraries(${PROJECT_NAME} PUBLIC ... dl)

标签: c++ linux boost cmake


【解决方案1】:

把我的评论变成答案:

因为boost::stacktrace 需要dl 库(参见Boost requirements),所以dl 库应该在 Boost stacktrace 库被链接之后被链接。为确保此顺序,请在 target_link_libraries() 调用的末尾链接 dl 库(可能还有 pthread):

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
)

【讨论】:

  • 为什么也许 pthread 也是如此?
  • @kevr OP 在示例中也使用了-pthread 标志,因此这可能还需要在 Boost 堆栈跟踪库链接之后移动。
  • 您也可以使用 ${CMAKE_DL_LIBS} 变量。
猜你喜欢
  • 2022-11-24
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多