I am writing a C++ program for an embedded device, and I want to compile it without libstdc++, exceptions, and dynamic memory allocation.
Example program:
#include <stdio.h>
class A
{
public:
virtual ~A() {}
virtual void Foo() = 0;
};
class B : public A
{
public:
virtual ~B() {}
virtual void Foo() override{}
};
int main()
{
B b;
return 0;
}
Immediately I ran into the following errors.
$ gcc src.cpp -static -fno-rtti -fno-exceptions -std=c++11
/tmp/ccd0Wydq.o: In function
A::~A()': src.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x29): undefined reference to
operator delete(void*)' /tmp/ccd0Wydq.o: In functionA::~A()': src.cpp:(.text._ZN1AD0Ev[_ZN1AD5Ev]+0x20): undefined reference to
operator delete(void*)' /tmp/ccd0Wydq.o: In functionB::~B()': src.cpp:(.text._ZN1BD2Ev[_ZN1BD5Ev]+0x35): undefined reference to
operator delete(void*)' /tmp/ccd0Wydq.o: In functionB::~B()': src.cpp:(.text._ZN1BD0Ev[_ZN1BD5Ev]+0x20): undefined reference to
operator delete(void*)' /tmp/ccd0Wydq.o:(.rodata._ZTV1A[_ZTV1A]+0x20): undefined reference to `__cxa_pure_virtual' collect2: error: ld returned 1 exit status Makefile:2: recipe for target 'all' failed make: *** [all] Error 1
I understand why __cxa_pure_virtual
is needed, but couldn't for the life of me get why I need a delete
implementation.
I perform no new
or delete
operations in the code, why would it be needed?
When implementing both functions to satisfy the linker's demands, it seems that neither are called (as expected).
Is there a way to avoid implementing these functions?
assert(1==0)
to be sure that they are not called. – Beethoven-flto
or-fwhole-program
, and-Wl,--as-needed
. – Frissell