I decided to try boost :: stacktrace and reproduced the simplest example from the boost documentation. I use qt-creator 4.8, Cmake 3.10, GCC 7.3, boost 1.69.0.

Code:

#include <boost/stacktrace.hpp> #include <iostream> void Foo() { std::cout << boost::stacktrace::stacktrace() << std::flush << std::endl; } int main() { Foo(); return 0; } 

Cmake script:

 cmake_minimum_required(VERSION 3.10) project(fast_test_3) add_definitions(-DBUILD_TYPE_="${CMAKE_BUILD_TYPE}") add_definitions(-DPROJECT_NAME_="${PROJECT_NAME}") set(CMAKE_CXX_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread ") # set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wall -pthread -g -DDREAL_DEBUG -DDREAL_TRACE -fno-inline") set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -Wall -pthread -Os -DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -Wall -pthread -O3 -DNDEBUG") set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -Wall -pthread -O2 -g") set (Boost_USE_STATIC_LIBS ON) set (Boost_USE_MULTITHREADED OFF) find_package(Boost REQUIRED system) include_directories (SYSTEM ${Boost_INCLUDE_DIR}) link_directories(${Boost_LIBRARIES}) add_executable(${PROJECT_NAME} "main.cpp") target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES}) 

At compile time, the compiler produces four errors:

  • /.../main.cpp.o:-1: In function `boost :: stacktrace :: detail :: to_string [abi: cxx11] (boost :: stacktrace :: frame const *, unsigned long) ':

  • /.../main.cpp:-1: error: undefined reference to `dladdr '

  • /.../main.cpp:-1: error: undefined reference to `dladdr '

  • : -1: error: collect2: error: ld returned 1 exit status

As far as I know, boost :: stacktrace is a hederonly library and I cannot understand what the problem is?

UPD:

During the discussion, the question arose whether this hederonly library? I provide a link to the place in the documentation where this moment is negotiated.

  • So far I could not find the answer, but about: << std :: flush I wanted to note that std :: endl clears the stream at the same time, so std :: flush is completely unnecessary writing. - AR Hovsepyan
  • 2
  • @ARHovsepyan, when I debug remotely on arm machines, sometimes the output without std :: flush did not work, even if there was an endl. Since then, there is such a habit when I write under Linux. Perhaps I did something wrong then. Anyway, thanks for the comment. - mrFieldy
  • one
    Apparently you need to link -ldl - VTT

1 answer 1

Killing a lot of time, I solved this problem. In general, as it turned out, the DL library is required to build something with boost :: stacktrace on GNU (thanks to @VTT for the help). But in order for CMake to collect everything normally, it is necessary to add the $ {CMAKE_DL_LIBS} variable to target_link_libraries (the presence or absence of the compilation flag -ldl does not affect anything, I don’t know why). The final script looks like this:

 cmake_minimum_required(VERSION 3.10) project(fast_test_3) add_definitions(-DBUILD_TYPE_="${CMAKE_BUILD_TYPE}") add_definitions(-DPROJECT_NAME_="${PROJECT_NAME}") set(CMAKE_CXX_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread ") # set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wall -pthread -g -DDREAL_DEBUG -DDREAL_TRACE -fno-inline") set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -Wall -pthread -Os -DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -Wall -pthread -O3 -DNDEBUG") set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -Wall -pthread -O2 -g") set (Boost_USE_STATIC_LIBS ON) set (Boost_USE_MULTITHREADED OFF) find_package(Boost REQUIRED system) include_directories (SYSTEM ${Boost_INCLUDE_DIR}) link_directories(${Boost_LIBRARIES}) add_executable(${PROJECT_NAME} "main.cpp") target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${CMAKE_DL_LIBS})