Page 1 of 1

Pure Virtual Functions

Posted: Mon Jun 21, 2004 11:00 pm
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.

RE:Pure Virtual Functions

Posted: Mon Jun 21, 2004 11:00 pm
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.

RE:Pure Virtual Functions

Posted: Tue Jun 22, 2004 11:00 pm
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 :(

RE:Pure Virtual Functions

Posted: Tue Jun 22, 2004 11:00 pm
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.

RE:Pure Virtual Functions

Posted: Tue Jun 22, 2004 11:00 pm
by Starr
I'm pretty sure virtual functions require runtime support, since their purpose is runtime polymorphism.

RE:Pure Virtual Functions

Posted: Wed Jun 23, 2004 11:00 pm
by Shmooze
They do, hence the whole 'what function do i need' thing.

RE:Pure Virtual Functions

Posted: Wed Jun 23, 2004 11:00 pm
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");
}

RE:Pure Virtual Functions

Posted: Thu Jul 15, 2004 11:00 pm
by Legend
If you manage to call it, you have a problem in your build process ..