Giovanni's Diary > Subjects > Programming > Gists >
C / generate-debuginfo
Separate debuginfo and executable in two different files.
# generate-debuginfo
# ==================
#
# This is a tiny example that shows how to have a debug-stripped
# executable and a separate debug information file that can be
# loaded by GDB. This is useful since you may not want to ship
# the executable with debuginfo, but you may optionally provide
# the info seperately.
#
# Credits: https://davidisaksson.dev/posts/detached-debug-symbols/
#
# Build executable
$ cat > main.c << EOF
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
EOF
$ gcc -g -o main main.c
# Separate debug symbols from the executable
$ objcopy --only-keep-debug main main.debug
$ strip --strip-debug maon
$ ls
main main.c main.debug
# Debug with GDB
$ gdb --exec=main --symbols=main.debug