gpt4 book ai didi

c++ - 尝试将 boost::stacktrace 添加到 CMake 项目时生成错误

转载 作者:行者123 更新时间:2023-12-01 14:43:31 25 4
gpt4 key购买 nike

我正在尝试通过 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

最佳答案

把我的评论变成答案:

因为 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
)

关于c++ - 尝试将 boost::stacktrace 添加到 CMake 项目时生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60039866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com