尝试将 boost::stacktrace 添加到 CMake 项目时出现构建错误

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

本文关键字:项目 错误 构建 CMake boost stacktrace 添加      更新时间:2023-10-16

我正在尝试通过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(
  • 增强版本 1.68.0

下面是一些代码:

CMakeList.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}
)
#-------------------------------------------------------------------#

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

将我的评论变成答案:

由于boost::stacktrace需要dl库(请参阅提升要求(,因此应在链接 Booststacktrace后链接dl库。为了确保这种排序,请在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
)