Skip to main content
gamedev.net
🔒 Locked

[C++][SOLVED] Linker errors with mangled names

Started by Decrius Jun 13, 2009 at 2:02 PM 2 replies 1.5k views
Original Post
Decrius
Decrius
AFAIK this code worked before I reinstalled WinXP + CodeBlocks + MingW (Windows needs to be reinstalled annually for obscure reasons to keep it up to speed), but I *could* have modified something in the code that produces this error... Anyways, ld.exe is giving me errors like:
obj\Release\main.o:main.cpp:(.text+0x1477): undefined reference to
`_ZNK8DaevsGUI7PointerINS_5Fonts9FreeType2EEcvNS0_IT_EEINS_4FontEEEv'
I do link to the library, naturally. I tried using C++filt to demangle the name and track what exactly is giving this linker error. Rebuilding both the library and executable didn't solve it. Typically, DaevsGUI and DaevsGUI::Fonts are namespaces, DaevsGUI::Pointer is a templated class which takes class DaevsGUI::Fonts::FreeType2 as templated type. DaevsGUI::Fonts::FreeType2 is a derived class from DaevsGUI::Font (base class). What is causing the problem? I suspect a member function from DaevsGUI::Fonts::FreeType2 or DaevsGUI::Font, but I'm really unsure. Also I don't see any reason why a linker error appears anyways! If code is required to solve the problem, I can give it, but it might be lengthy... I use the latest GCC stable release. Thanks in advance! [Edited by - Decrius on June 15, 2009 12:47:17 PM]
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
frob
frob
Quote:
What is causing the problem? I suspect a member function from DaevsGUI::Fonts::FreeType2 or DaevsGUI::Font, but I'm really unsure. Also I don't see any reason why a linker error appears anyways!

The linker error is an "undefined reference".

The cause is simple to explain:

The file main.cpp has got a function declaration. The compiler generated an object file from that source file (obj\Release\main.o) that is completely valid. The object file says "Call this function. I know the symbol name, but I don't know where it is."

Part of the linker's job is to take names from the object file and convert them into addresses in the final executable. The linker is trying to match that function but cannot find it.

The linker is complaining that it looked for the symbol `_ZNK8DaevsGUI7PointerINS_5Fonts9FreeType2EEcvNS0_IT_EEINS_4FontEEEv' but couldn't find any references to it in any library it searched.

Incidentally, you can use your c++filt tool to automatically demangle the name.




That kind of error could appear for many reasons.

For templates, you generally must include the function definition (the function body) inline with the header file. There are a few ways around this restriction but they require a deeper knowledge of the language and your compiler.

For non-templates, the most common is that the library with the function is not being included in the linker's search. Another very common reason is that the function is declared in a header but there is no matching definition in a cpp file.

There are countless other reasons the linker might not find the name, but those are the most common.
Decrius
Decrius
The code has worked before. All classes and their member functions and data members are declared in the header file. Implementation in the source file. Templated classes are entirely in the header file. I compile each source file into one .a library file which I link against in the executable, which also includes all headers. Just a usual set up.

C++filt doesn't work for me, it asks to pass the symbol, so I do, it just returns the symbol itself, no converting (demangling). I use it as such:
c++filt _ZNK8DaevsGUI7PointerINS_5Fonts9FreeType2EEcvNS0_IT_EEINS_4FontEEEv

What are the less common reasons why the linker might throw this error?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Decrius
Decrius
Can someone who got C++filt to work, try and demangle this for me?

_ZNK8DaevsGUI7PointerINS_5Fonts9FreeType2EEcvNS0_IT_EEINS_4FontEEEv

That would be really helpfull, since C++filt is not. Under no circumstance it gives me a demangled name =/

EDIT:
After some hopeless minutes staring at this:
http://www.codesourcery.com/public/cxx-abi/abi-mangling.html

and playing around with the code, it seems this is giving the trouble:
template <class P> operator Pointer() const;

which implementation I removed, but I forgot the declare to be removed as well!

All because I saw the 'cv' combination (type conversion), then the derived and base class, and then noticing that if the templated class was passed to another class/function, it was b0rked...lol. Bugs are NASTY things (especially because it's us humans being imperfect ^^)!

[Edited by - Decrius on June 15, 2009 12:05:10 PM]
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.