Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug C++0x programs in MacPorts gcc 4.5?

I have a simple c++ program I am trying to debug, but gdb cannot find the object file for the libraries (or no debug info is available), and it does not seem able to find the debug symbols for my executable either.

I am on OSX 10.5.8, with macports, and I compile my code with

g++-mp-4.5 -Wall -pedantic -std=c++0x -g -ggdb -I/opt/local/include -L/opt/local/lib -lgsl -static-libstdc++ MCMC-simplex.cpp -o mcmc

(there is only one file, and g++-mp-4.5 is the macports executable for gcc/g++ 4.5 )

When I try running gdb on the resulting executable, I get many error messages (at startup) of the form

warning: Could not find object file "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_lang_gcc45/work/build/i386-apple-darwin9/libgcc/trunctfdf2_s.o" - no debug information available for "../../../gcc-4.5.0/libgcc/../gcc/config/soft-fp/trunctfdf2.c".

which to me indicates that macports has a bug during its build (it seems like gdb is looking for the object files in the temporary build directory).

I should add that when I try to see my programs listing in gdb (the one provided by Apple), it tries to look for a random .s file in /var/tmp, which to me sounds like an assembler file. That is why I say it does not seem able to find the debug symbols for my program either.

When I try MacPorts gdb 7.1, I get

warning: `/var/folders/Xa/XaqHO9PeEC8K-Nrd0L9xWk+++TM/-Tmp-//cc2IvFto.o': can't open to read symbols: No such file or directory. (no debugging symbols found)...done.

and none of the many error messages that Apple's gdb gives out (although the end result is the same).

Has anyone come across this problem, and came up with a solution?

like image 727
Marcus P S Avatar asked Jul 28 '10 14:07

Marcus P S


2 Answers

Unlike other UNIXen, on MacOS the debug info is not linked into the executable. Instead, the executable has a list of object files which were linked into it, and the debugger looks for debug info in these individual object files.

If you remove the object files, then you can't debug.

When you compile and link the executable in "single step", GCC does this:

  1. Create assembly file /tmp/[random-string].s
  2. Assemble it into /tmp/[random-string].o
  3. Link /tmp/[random-string].o with crt0.o, libc, etc. into mcmc executable.
  4. Remove /tmp/[random-string].o and .s

It is that last step which prevents you from debugging.

Solution:

g++-mp-4.5 -Wall -pedantic -std=c++0x -g -ggdb -c MCMC-simplex.cpp
g++-mp-4.5 MCMC-simplex.o -lgsl -static-libstdc++ -o mcmc

This will leave MCMC-simplex.o in the current directory, and will allow GDB to find debug info in it.

like image 172
Employed Russian Avatar answered Sep 18 '22 18:09

Employed Russian


Well, another "trick" to keep going with a single compile-and-link step would be to add
-save-temps=obj
to your g++ command line so that

4 Remove /tmp/[random-string].o and .s

is actually sort of not performed (actually you end up having the canonical SOURCE.o and SOURCE.s files in the directory where you're building instead of RANDOM-STRING.[os] in some temp folders, but from the point of view of locating debugging symbols that's fine

like image 30
abigagli Avatar answered Sep 21 '22 18:09

abigagli