Linux GCC: debug libraries and building adjusted GCC configuration

Hi, all!

I'm new in Linux development, starting C++ development for CentOS 5.5 in C++. I'm a WIndows programmer and used to C++ development in Visual Studio. So, I have some Linux-specific related questions and would appreciate any assistance. Time to time I'm going to post some questions.

I got Centos 5.5 in a VmWare image. So, GCC was configured by somebody, maybe in the default way. I need to adjust or rebuild it for my needs.

It seems GCC was installed via rpm packages:
rpm -qa | grep gcc


Here is my GCC configuration:
  version 4.1.2 20080704 (Red Hat 4.1.2-48)
  Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj- --with-cpu=generic --host=i386-redhat-linux
Thread model: posix

1. The first thing I faced with: there aren't any debug libraries (*.a, *.so) for default libraries.
I build a hello world application as:
COMPILE=g++ -c  "-D_GLIBCXX_DEBUG" -g -O0 -fno-inline -o "$(OUTDIR)/$(*F).o" $(CFG_INC) $<
LINK=g++  -g -o "$(OUTFILE)" $(ALL_OBJ)

ldd shows these dependences:
       =>  (0x00fd7000)
       => /usr/lib/ (0x02839000)
       => /lib/ (0x00c54000)
       => /lib/ (0x02702000)
       => /lib/ (0x00ad9000)
                /lib/ (0x00aba000)

All the libs are release. So, I can't step into them in the debugger.

Q1.: How to add debug default libraries and their sources to GCC installation?

Q2.: How to link properly my application in DEBUG configuration to debug libraries? In Visual Studio in debug conf. an application is linked to debug versions of std. libraries suffixed by "d". What's approach is used in Linix GCC?

Q3. What could you say about my GCC configuration shown above? Definitely, I would rebuild GCC with C, C++ turned on languages only. Any other advises about the configuration? I need c/c++, x86 32/64 bit, with debug std libs configuration.
Also, where could I find a step by step instruction how to build and install GCC configuration I need?

With best regards,
Alexey FedorovSoftware developerAsked:
Who is Participating?
Duncan RoeConnect With a Mentor Software DeveloperCommented:
Gcc builds with debug on by default, but the installer strips debug information. To get around that, do a full source build and install, then copy by hand those items you wish to debug.
There is no change to the link procedure.
I would leave it as_is: it's very hard to get ada back if you drop it. You need to get the srpms e.g. gcc-c++-4.1.2-48.el5.srpm. Then build using rpmbuild. Verify the installed gcc is smaller than the built one in the build directory. That will be because of debug info being stripped. Copy it. Note that gcc is a front-end to the actual compilers, which have names like cc1, cc1plus, cc1obj and so on. You need to copy these as well. They will be in a revision-dependent directory (on my system they are in /usr/libexec/gcc/i486-slackware-linux/4.4.4, but yours will be different).
Duncan RoeSoftware DeveloperCommented:
Only and are part of the gcc distribution. The others, which get used far more often, are part of glibc. You can (I have) rebuild glibc so that debugging is available, but why do you want to? (if you don't mind my asking). The libraries end up with the same names but contain debug information which makes them considerably bigger but not AFAIK much slower. Of course if you drop the optimising level to give cleaner debugging then they will be somewhat slower.
Alexey FedorovSoftware developerAuthor Commented:
Thanks, Duncan.

"but why do you want to?" - sometimes stepping into libs may be helpful because unmanaged C++ code is prone to memory allocation and corruption errors causing fault in libs. Also, on Windows in MS Visual Studio we have this ability without any extra effort, so I consider that as usual feature.

I understand the simplest way: to download GCC source code and build it in a separate dir. Than, copy debug libs and their sources manually into my GCC installation dirs. And, the same steps I should do to GLIBC. Am I correct?

You told "Gcc builds with debug on by default". I thought it builds debug when --enable-libstdcxx-debug is specified:  Isn't it?

Where debug libraries must be reside at, and, why I don't need to do special setting to link to them?

On WIndows for a WIn32 C++ console app in Visual Studio 2010 it looks as follow.
  dumpbin /dependents shows:
     MSVCR100.dll for release build, and

     MSVCR100D.dll for debu build.

MSVCR100 resides in WIndows/System32 folder. It exists there in different versions with different names: msvcrt.dll, msvcrt20.dll, msvcrt40.dll plus debug versions with suffix 'd'. Compiler automatically links to 'd' version in debug build configuration and to release version in release build. So, it works very clearly and gives ability to step into default stuff like "printf" in debug mode, that is convenient.

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Duncan RoeConnect With a Mentor Software DeveloperCommented:
unmanaged C++ code is prone to memory allocation and corruption errors causing fault in libs - yes I am very familiar with that ;)
The idea of stepping into system libraries to address such problems is, I must confess, new to me but if it works for you then go for it. The problem with debugging the library is, of course, that the code in the library is usually fine: the bug that caused the storage arena corruption executed some time previously.
People unable to otherwise solve such problems generally use a package to help them, such as (free) Valgrind, (free) DUMA (formerly Electric Fence) or (paid for) IBM Rational Purify

Yes download gcc source code and build it in a separate dir. No need to move the source - use gdb dir command (you would need to anyway).
Building glibc is not for the faint-hearted. Best is to use the distributor's build process (i.e. get the SRPM). Be sure to run the self-tests. Be especially sure to run the self-tests if you change anything.

The page you refer to says the same as I did: The default optimizations and debug flags for a libstdc++ build are -g -O2. So debug (-g) is on by default. But the page does explain how to improve on that, as you mention (thanks for that btw - I did wonder why I couldn't debug new() :)

As per the web page, you can put debug libstdc++ in a special dir. glibc you can not. If you install a debug version then every process gets to use it. For that reason, there is no special linking.

Linux does things differently from Windows.
Alexey FedorovSoftware developerAuthor Commented:
Hi, Duncan!

I've found there are debu rpm packages: glibc-debuginfo, glibc-debuginfo-common.
dcesariConnect With a Mentor Commented:
Concerning Q1, nowadays rpm-based distributions (at least Fedora which I use, and consequently CentOS) provide, for most library packages, a corresponding  *-debuginfo package which contain separate debugging symbols and source files for the shared libraries, these symbols are usually not used, but they are loaded by gdb, so that you can step into library functions.

So, if you wish to step into a function which is in a library belonging, e.g., to zlib package, you have to install a package like that (as root):

yum install zlib-debuginfo

then launch gdb as usual. The packages you need are probably glibc-debuginfo and gcc-debuginfo.

Before doing this you may have to enable the debuginfo repositories by setting "enabled=1" in the corresponding entries in /etc/yum.repos.d/*.repo, if they are disabled.

I anyway agree with the previous posts that it may be quite useless to step into libc functions, it is usually enough to know which is the function call that generates a segfault or whichever error.

Alexey FedorovSoftware developerAuthor Commented:
Hi, Dcesari!

I turned on:
  name=Extra Packages for Enterprise Linux 5 - $basearch - Debug

   name=Extra Packages for Enterprise Linux 5 - $basearch - Source

But, couldn't find glibc, gcc debuginfo. I found them as separate roms at

By the way, I updated glibc from "update" repository: yum update glibc.

Alexey FedorovSoftware developerAuthor Commented:
How could I find a repository by package?
Alexey FedorovSoftware developerAuthor Commented:
I've found out here

I need to add "debuginfo"  repository. I can do it manually in /etc/yum.repos.d/  
how it is described here:

I wonder how to add it via yum without manual editing the config.
Duncan RoeSoftware DeveloperCommented:
I run Slackware, so am not especially familiar with Yum. It looks to me from the Web pages that you may not have to manually edit the config. Instead it seems you could create a new file such as /etc/yum.repos.d/CentOS-Debug.repo. Usually the way xxx.d dirs work is that all contained files are effectively concatenated - sometimes only those files that have execute access. Do an ls -l /etc/yum.repos.d to see if the execute bit is required. You might even find that the debuginfo file is there already, without execute.
In the new file, you would have to specify enable=1 for Centos 5
Duncan RoeSoftware DeveloperCommented:
Actually enabled=1
Alexey FedorovSoftware developerAuthor Commented:
I ended up with adding the repository.

1. Added CentOS-Debug.repo into /etc/yum.repos.d/ dir:

#Debug Info
name=CentOS-$releasever - DebugInfo
# CentOS-5

2. Excecuted:
yum --disablerepo "*" --enablerepo "debuginfo" install glibc-debuginfo glibc-debuginfo-common gcc-debuginfo

Now I compiled a test program with: "-D_GLIBCXX_DEBUG" -g -O0 -fno-inline
But, I still unable to step into default functions (for inst. wprintf). Why?
Alexey FedorovSoftware developerAuthor Commented:
One more question. in MS Visual Studio there is a nice feature: at starting debugging up it shows in Output window all the loaded libs into debugged process and reports whether symbols are found or not for each lib.

How could I i view the same in SlickEdit 15 attached to gdb under Linux?
Duncan RoeSoftware DeveloperCommented:
What files did Yum install and where did it put them? "-D_GLIBCXX_DEBUG" is unlikely to help you because you will after all need to tell the linker to do something special if indeed Yum has put the debug libraries in some special dir. (Red Hat (and derivatives like CentOS) tend to be rather more profligate with disc space than Slackware, at least AFAICS).
Duncan RoeSoftware DeveloperCommented:
Sorry - I have not yet tried to use SlickEdit
Alexey FedorovSoftware developerAuthor Commented:
Maybe, some gdb command to list loaded modules?
Duncan RoeSoftware DeveloperCommented:
info sharedlibrary
Alexey FedorovSoftware developerAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.