Home > Net >  Why gcc -g doesn't work with multiple files
Why gcc -g doesn't work with multiple files

Time:11-10

To debug my C code I compile it with the -g flag and use lldb to see where my seg fault is for example.
I use the -g flag so the output of lldb is in C not Assembly.
but now I have a multiple files project and lldb shows only Assembly even tho I'm using the -g flag, it's like the -g flag applies only to one file.
Example:

gcc -g example.c
lldb a.out
>run
   I get c code here
gcc -g example1.c example2.c main.c
lldb a.out
>run
   I get assembly code here

Can anyone tell me what I'm I missing here?
and how can I get c code in lldb.

Thanks in advance.

CodePudding user response:

When you just run the program you shouldn't be getting code at all.

You will be getting code if the program stops running. Then you need to look at the call stack to make sure you're actually in your own code.

If you're in library code then it will likely not have source available and you'll get assembler code. Go up the call-stack until you reach your own code.

CodePudding user response:

GNU’s documentation for the ‘gcc -g’ says

Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.

Notice that it makes no mention of either C or assembler.

I imagine that in your first example the error was in your C code; in your second example the error was in a library, such as stdio, for which the debugger doesn’t have C source.

A segmentation fault corresponds to an invalid address. This might mean that you passed invalid data to a library that was expecting a pointer, or you passed a pointer to a buffer but an incorrect length.

A typical error that might cause this is passing a value (v) to a library that is expecting a pointer (&v).

  • Related