Pure Virtual Functions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Shmooze

Pure Virtual Functions

Post by Shmooze »

I am attempting to write a C++ kernel using GCC, but when i try and create classes with pure-virtual functions i get the following linker error:

"undefined reference to `___cxa_pure_virtual'"

What does this function actually do, and whats its prototype?
Cheers, Shmooze.
GT

RE:Pure Virtual Functions

Post by GT »

I believe it's supposed to spit out some sort of error if you somehow manage to call it.  In my "support.c" source that I link with C++ programs that don't have library support, I just have this:
        
void __pure_virtual(void)
{
}

And that appears to satisfy the linker.  You may need to change the name to ___cxa_pure_virtual, depending on the version of GCC you're using at the moment.
Shmooze

RE:Pure Virtual Functions

Post by Shmooze »

Thanks! Its strange though, I thought that the only C++ language elements needing extra support functions were new/delete, exceptions, and constructor/destructors at startup/shutdown... clearly I was wrong :(
Shmooze

RE:Pure Virtual Functions

Post by Shmooze »

BTW, the definition I used (for DJGPP) was:

extern "C" void __cxa_pure_virtual(void)
{
}

as I couldn't work out the correct name without forcing the compiler not to mangle the name.
Starr

RE:Pure Virtual Functions

Post by Starr »

I'm pretty sure virtual functions require runtime support, since their purpose is runtime polymorphism.
Shmooze

RE:Pure Virtual Functions

Post by Shmooze »

They do, hence the whole 'what function do i need' thing.
jocko

RE:Pure Virtual Functions

Post by jocko »

runtime support is not really needed, the compiler will emit the vtables and generate the proper code to do the lookups.  that function is what gets called if you somehow call a pure virtual function, which should never happen under normal circumstances.

my definitions is:

extern "C" void __cxa_pure_virtual() {
// FATAL ERROR! we just called a pure virtual function somehow...
panic("pure virtual function called in kernel!\n");
}
Legend

RE:Pure Virtual Functions

Post by Legend »

If you manage to call it, you have a problem in your build process ..
Post Reply