Saturday, May 12, 2007

Undefined symbols in C++

In C++ programming, we sometimes encounter "undefined symbols" problem during compilation or dlopen. The name of undefined symbols looks obfuscated. E.g.:
Unable to dlopen(test.so): test.so: Undefined symbol "_ZN6moduleD2Ev"
You may wonder why the symbol looks so ugly. Actually, this conversion of symbol is called name mangling. C++ supports polymorphism, this means functions can have same name but different types and numbers of parameters. Therefore, compiler cannot just use the function name as the symbol. Instead, both function name and parameter types should be included in symbol naming. Name mangling is the technique to encode a function name and parameter types into one symbol.

To translate the mangled symbols to more meaningful text, we can use the c++flit utility. E.g.
ahlam@oxygen:~$ c++filt _ZN6moduleD2Ev
module::~module()
Now, you know "_ZN6moduleD2Ev" is the destructor of class module.

1 comment:

MeduZa said...

Wow I found my error thanks to you and the command c++filt

thanks men