Debug Flag With GCC (compile phase / link phase)
Asked Answered
A

2

7

I have a this Makefile

application=<somebinaryname>
CXXFLAGS=-g -std=c++14 -Wall -Werror -pedantic
LDFLAGS=-g

auto: $(application)

$(application): main.o aaa.o aab.o aba.o baa.o
        $(CXX) $(LDFLAGS) -o $@ $^

%.o: %.cpp
        $(CXX) $(CXXFLAGS) -c $< -o $@

# rest of Makefile not relevant to my question

Can someone please tell me if the -g option is supposed to go during the compilation phase (with CXXFLAGS) or during the link phase (with LDFLAGS)? I looked for examples and documentation everywhere, but they all have very trivial examples like (even the manpage):

gcc -g -o binary source.cpp

I get that, but it doesn't tell me much.

Any more clarity on this?

Adorne answered 18/10, 2016 at 2:16 Comment(2)
Possible duplicate of How to generate Debug symbols with Makefile for C? [Linux]Daredevil
Asked myself the same question. The answers in #1937920 are mixed. One says give -g to both the compiler and linker, two say only give -g to the compiler.Kaminski
B
3

-g produces debugging information. Compile your C program with -g option. This allows the compiler to collect the debugging information. Then you can use gdb to debug the binary.

Some useful links

http://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/

https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html

GCC -g vs -g3 GDB Flag: What is the Difference?

Ballocks answered 18/10, 2016 at 6:39 Comment(1)
I apologize. My question was probably not sufficiently clear. Your links repeat the information that is already present in about 5000 different documents. I know that -g produces unoptimized debug binary with all the symbol names intact. I know what debugging is, what a debug file is, why it is used, how it is used and have used it for many years. What I am uncertain however is if '-g' or '-ggdb' should go during the compile phase (as in along with '-c') or during the link phase? So again, in my specific Makefile example, should it go with CXXFLAGS or LDFLAGS or both like I have it?Adorne
K
0

https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html The gcc documentation speaks about -g in the compilation phase. I deduce from that, it is not needed in the link phase. Oddly enough though, the linker does not complain/warn when you it -g.

Kaminski answered 23/8, 2022 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.