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?

Novice User
  • 91
  • 1
  • 3
  • 5
  • Possible duplicate of [How to generate Debug symbols with Makefile for C? \[Linux\]](https://stackoverflow.com/questions/1937420/how-to-generate-debug-symbols-with-makefile-for-c-linux) – zingdle Jul 29 '19 at 03:02
  • Asked myself the same question. The answers in https://stackoverflow.com/questions/1937420/how-to-generate-debug-symbols-with-makefile-for-c-linux are mixed. One says give `-g` to both the compiler and linker, two say only give `-g` to the compiler. – Bart Aug 23 '22 at 09:44

2 Answers2

2

-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?

Community
  • 1
  • 1
ganeshredcobra
  • 1,881
  • 3
  • 28
  • 44
  • 9
    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? – Novice User Oct 18 '16 at 19:49
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.

Bart
  • 179
  • 1
  • 9