开发者

g++ vs gcc, linking problem with a static library (.a)

I'm trying to link a static library (.a) file with a .o file which supposedly uses the symbols from the library. However, when using gcc - the normal linker error comes up, regardless of using the .a file as

gcc -L. a.c staticlib.a

However, the same command works with g++ flawlessly.

Why is this happening ?

I can see that the .c file is totall开发者_JAVA百科y legal c ( and hence c++ ), but then why isn't gcc able to detect symbols in the the library ?

Tried finding the symbols in the library using objdump, was able to find closely resembling symbols, but not exact ones. e.g:

Got 00000000000000b0 g F .text 000000000000004e _*Z15PhttsFn_InitTTSPh* for the symbol *PhttsFn_InitTTS*

Can someone please explain this phenomenon ? I've also checked the architecture the library file was compiled for, and it's the same as my architecture.

Thanks!


C++ uses something called name mangling, in order for namespaces, overloaded function names, etc, to get unique symbols in the compiled object file.

Your C code refers to a symbol PhttsFn_InitTTS explicitly. Now if compiled as a C, it will produce that very symbol name. However, since C++ needs to deal with all these different variations of the same name (e.g. overloading, with different parameter lists), it creates a 'mangled' version encoding namespace, and parameter types. In your case it was mangled to Z15PhttsFn_InitTTSPh, basically saying no namespace and no parameters. (I reckon Z15 means 15 character name; followed by no parameter list).

Invoking GCC as gcc allows it to pick the file format itself, based on file extension (.c -> C, .cc or .cpp, etc -> C++). Invoking it as g++ forces C++ mode.

Your .a-file is obviously compiled using C++, as it exposed that mangled symbol.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜