尝试将 boost::stacktrace 添加到 CMake 项目时出现构建错误
Build errors while trying to add boost::stacktrace to CMake project
我正在尝试通过CMake将boost::stacktrace
库添加到我的项目中。CMake 很好地找到了所有必需的库,但是当我调用boost::stacktrace::stacktrace()
将堆栈信息打印到std::cout
中时 - 它给了我以下错误:
undefined reference to 'dladdr'
我已经尝试将-ldl
编译标志添加到CMAKE_CXX_FLAGS
编译标志中,看看它是否有帮助,但它不起作用。我知道我错过了一些必需的软件包,但我不知道是哪个。也许你可以建议我。
- (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
)
相关文章:
- 项目错误:QT中的未知模块:Mac上的图表
- 欧拉项目#8:C++错误的答案(为一个小的炮制输入给出正确的答案)
- 项目错误:无法解析'macosx'的 SDK 路径
- 混合模式程序集是针对版本 'v2.0.50727' 构建的 ...本机C++/Visual C++/C# 项目错误
- windows 10上的wxWidgets 3.1.0项目错误
- 将 Sqlite3 添加到项目错误 C1853 预编译头文件来自以前的版本
- Visual Studio 2015 c++ 项目错误 c++11 标准
- 用中断函数将Keil5C项目转换为C++项目错误
- 无法在vs2012中生成任何c++项目.错误LNK1327:rc.exe
- Oculus Rift VR - 示例项目错误
- 项目错误:QT 中的未知模块:webkitwidgets
- VTK+QT项目:错误LNK1169:找到一个或多个多重定义符号
- Microsoft Visual Studio-项目错误
- 电话簿项目错误.在编译器中并没有发现错误,但当它运行很长时间后就会退出
- Qt创建者-项目错误:Xcode设置不正确.您可能需要通过运行/usr/bin/xcodebuild来确认许可协议
- VSU 2012-在其他计算机上中继项目错误(SQLCE)
- 项目错误:QT中的未知模块:蓝牙
- 项目错误:未知模块(s)在QT: qml快速
- ReadDirectoryChangesW WIN32 API项目错误
- 1.项目错误:QT: webkitwidgets中的未知模块