CMake save debugging information deleted

This is the usual practice of compiling with debugging symbols, and then splitting the binary file with objcopy into the release executable and the file with debugging information (then transferring it to separate packages or saving it to the symbol server).

How to properly separate debug symbols in CMake? I saw only some discussions and incomplete code examples.

The platform is Linux and GCC.

+8
gcc linux cmake objcopy
source share
1 answer

CMake does not have direct support for this, but you can use some of the POST_BUILD and INSTALL steps to achieve the desired result. However, it is worth noting that using objcopy is not the only way to do this. You can also use the assembly identifier, and it might be easier to implement with CMake.

Instead of repeating everything here, a pretty good description of your options and methods that was sent to the CMake mailing list years ago by Michael Hertling. I will just choose a working alternative here for reference, but I recommend reading this link. The GDB documentation also contains an even more complete discussion of the two alternatives , which should fill in any remaining gaps regarding the two approaches (debug link versus assembly - I would). Here's a general approach to building-id Michael (the build identifier is explicitly indicated in his example, read the links to the articles to explain what he should represent):

 CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR) PROJECT(BUILDID C) SET(CMAKE_VERBOSE_MAKEFILE ON) SET(BUILDID "abcdef1234") STRING(SUBSTRING "${BUILDID}" 0 2 BUILDIDPREFIX) STRING(SUBSTRING "${BUILDID}" 2 8 BUILDIDSUFFIX) FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n") ADD_EXECUTABLE(main main.c) SET_TARGET_PROPERTIES(main PROPERTIES LINK_FLAGS "-Wl,--build-id=0x${BUILDID}") ADD_CUSTOM_COMMAND(TARGET main POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:main> ${CMAKE_BINARY_DIR}/main.debug COMMAND ${CMAKE_STRIP} -g $<TARGET_FILE:main>) INSTALL(FILES ${CMAKE_BINARY_DIR}/main.debug DESTINATION ${CMAKE_BINARY_DIR}/.build-id/${BUILDIDPREFIX} RENAME ${BUILDIDSUFFIX}.debug) 

Configure with CMAKE_BUILD_TYPE == debug and build; subsequently, cause

gdb -ex "install debug-file-directory." -ex "file main"

from CMAKE_BINARY_DIR, and you will read "no debugging characters found" as expected. Now issue "make install", call gdb again and read:

"Reading characters from ... /. Build-id / ab / cdef1234.debug"

As you can see, the debugging information file is associated with the deprived executable file only with the help of the assembly identifier; no visibility.

The above example uses the fact that the .debug file must be a normal executable file with debugging information that is not shared.

+2
source share

All Articles