In a freestanding context (no standard libraries, e.g. in operating system development) using g++ the following phenomenon occurs:
class Base {
public:
virtual ~Base() {}
};
class Derived : public Base {
public:
~Derived() {}
};
int main() {
Derived d;
}
When linking it states something like this: undefined reference to operator delete(void*)
Which clearly means that g++ is generating calls to delete operator even though there are zero dynamic memory allocations. This doesn't happen if destructor isn't virtual.
I suspect this has to do with the generated vtable for the class but I'm not entirely sure. Why does this happen?
If I must not declare a delete operator due to the lack of dynamic memory allocation routines, is there a work around?
EDIT1:
To successfully reproduce the problem in g++ 5.1 I used:
g++ -ffreestanding -nostdlib foo.cpp
Because of deleting destructors. That are functions that are actually called when you call
delete obj
on an object with virtual destructors. It calls the complete object destructor (which chains base object destructors — the ones that you actually define) and then callsoperator delete
. This is so that in all places wheredelete obj
is used, only one call needs to be emitted, and is also used to calloperator delete
with the same pointer that was returned fromoperator new
as required by ISO C++ (although this could be done more costly viadynamic_cast
as well).It's part of the Itanium ABI that GCC uses.
I don't think you can disable this.