Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Blogs > rainbowsally
User Name


Rate this Entry

Simple C: some ideas on how to find headers, names, and a name demangler tool

Posted 02-08-2012 at 04:23 AM by rainbowsally
Updated 08-06-2014 at 11:30 AM by rainbowsally

The simplest yet, I think.. at least the C program is.

  • tips on how to find C/C++ header paths and declarations
  • tips on how to find libs containing references and definitions
  • a super-simple program to demangle C++ names
Note: With even a simple automatic makefile generator such as the one built from sources in this blog (,
you can edit the makefile with a text editor to compile libs (an example coming up very soon), static/dynamic, change from 32-bit to 64-bit executables, add and remove files, copy headers, and/or binaries for installation, with a single command (such as 'makefile-creator <outputname>'.

So using makefiles is HIGHLY recommended especially if you are experimenting with C or C++ or developing applications where the files may change a lot.

And if you want a more full-featured makefile generator, check out AutoBake -- new and improved but still obviously the work of a backyard mechanic. :-)

The makefile-creator posted here (the first link near the top) will do the job nicely for this and other simple apps. And boy is this one SIMPLE! :-)
Now let's see what's up in today's Computer Mad Science or whatever. ;-)

If you can't find a header or want to look at it in an editor...
where GUESS_BRANCH is something like '/usr/include/*' or
'/usr/include/*/*' (low level system dependent headers).

If you need to find a library where a certain function is defined it can be found a couple of ways.

first check the development libs in /usr/lib
grep demangle /usr/lib/*.so 
grep demangle /usr/lib/*.a
then the development libs in /lib
grep demangle /lib/*.so 
grep demangle /lib/*.a
Note: If you suspect there is no development lib, add a splat after the *.so name so you can also check dso's with numeric extensions but be aware that they can't be linked with the -l switch in the Makefile LDFLAGS. They need to be listed by their full paths.

Here's what I get looking for 'demangle', for example, which we'll need for our demangling application that we create below.

grep demangle /usr/lib/*.so
  Binary file /usr/lib/ matches

grep demangle /usr/lib/*.a
  Binary file /usr/lib/libiberty.a matches
  Binary file /usr/lib/libstdc++.a matches
  Binary file /usr/lib/libsupc++.a matches

grep demangle /lib/*.so
  no hits

grep demangle /lib/*.so
  no hits
The best guess for a definition of this function would be libiberty.a for several reasons. One is that all the others are similar types of libs and probably have a common set of functions defined from somewhere and the oddball is libiberty, so that's most likely the common set of definitions.

We can verify this hunch rather than experimenting with the LDFLAGS in the makefile (easy enough to do but this is quicker) like this:
# Compare objdump of static and dynamic symbols:
  objdump -tT /usr/lib/ | sed '/demangle/!d'
  objdump -tT /usr/lib/libiberty.a | sed '/demangle/!d'
Here's what some of the symbols mean in these dumps.
  g = 'global'
  F = 'function'
  O = 'object' (or data/struct, etc.)
  .text = executable code section, read only
  .data = initialized data, read-write
  .bss  = uninitialized data, 
  *UND* = undefined, requires definition from elsewhere
libiberty is where our function "cplus_demangle_v3" is defined so that is the lib we'll link in a little program to demangle C++ names.

Here's the program:
// file: main.cpp 
// executable: demangle
#include <stdio.h>    // FILE stuff, printf(), sprintf(), etc.
#include <malloc.h>   // memory allocation
#include <string.h>   // strings and block comparisons and moves
#include <demangle.h> // the main function

int main(int argc, char** argv)
  // TODO: a more meaningful error handler
  if(argc < 2)
  // extern char * cplus_demangle (const char *mangled, int options);
  char* s = cplus_demangle_v3(argv[1], flags);
    printf("%s\n", s);
    printf("%s(...)\n", argv[1]); // or write a "can't demangle" warning.
  return 0;
If you use the makefile-creator utility from sources posted in this blog
create the makefile with the C++ option [you have added your own code to remind you of the syntax and do some error checks by now, right? :-) ] and add '-liberty' to the LDFLAGS with a text editor

Using this 'demangle' utility once it's built, here's what we get for a few of the mangled names in /usr/lib/
  # objdump sees: 000d3db0  w   DO .data 00000008  CXXABI_1.3  _ZTIw
  $> demangle _ZTIw
  typeinfo for wchar_t
  # And I have always wondered what a "C1ERKSs" was, haven't you?
  $> demangle _ZNSt14overflow_errorC1ERKSs
  std::overflow_error::overflow_error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
  # and now I regret having asked. :-)
Enjoy! :-)
Posted in Uncategorized
Views 1111 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 05:39 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration