Reputation: 1163
As per official instructions, to compile a program with debugging support you can run
g++ -std=c++11 -O0 -g -c -o program1.o program1.cpp
Now to do the same with a C program, it's just:
gcc -O0 -g -c -o program2.o program2.c
In order to link both types together, I could use:
g++ --std=c++11 -O0 -g -o program program.o program2.o
Then, to debug:
gdb program
gdb > run <PARAMS>
It worked completely after several attempts at tinkering with the compiler options (the above options are for a working version). In some cases the C symbols would load, but the C++ symbols would not.
Can someone shed some light as to what the recommended options are to enable debugging for non-trivial examples that mix several compiled languages? All of the documentation only refers to trivial examples.
Upvotes: 1
Views: 527
Reputation: 1163
In each compilation and linking step, add the -g
option to the compiler flags. -O0
is recommended too for debug builds so you don't get the compiler optimizing functions away. Being inconsistent may result in no debug symbols or partial debug symbols.
Upvotes: 0
Reputation: 10336
Note that if you use just the -g
option then the compiler will use operating system's native format, which can vary. You can explicitly specify the format instead, using other -g...
varieties (for example -gdwarf-3
or -g-stabs
). This allows you to guarantee that your object files will all have a consistent debug format, irrespective of where they were built.
You can also disable gdb
-only extensions by using this approach, should you wish to use other debuggers. See this for details.
Upvotes: 1