title: Compiling C++ without the delete operator
body: <p>I am writing a C++ program for an embedded device, and I want to compile it without libstdc++, exceptions, and dynamic memory allocation.</p>
  
  <p>Example program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  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;
  }
  </code></pre>
  
  <p>Immediately I ran into the following errors.</p>
  
  <blockquote>
    <p>$ gcc src.cpp -static -fno-rtti -fno-exceptions -std=c++11</p>
    
    <p>/tmp/ccd0Wydq.o: In function <code>A::~A()':
    src.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x29): undefined reference to
    </code>operator delete(void*)' /tmp/ccd0Wydq.o: In function <code>A::~A()':
    src.cpp:(.text._ZN1AD0Ev[_ZN1AD5Ev]+0x20): undefined reference to
    </code>operator delete(void*)' /tmp/ccd0Wydq.o: In function <code>B::~B()':
    src.cpp:(.text._ZN1BD2Ev[_ZN1BD5Ev]+0x35): undefined reference to
    </code>operator delete(void*)' /tmp/ccd0Wydq.o: In function <code>B::~B()':
    src.cpp:(.text._ZN1BD0Ev[_ZN1BD5Ev]+0x20): undefined reference to
    </code>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</p>
  </blockquote>
  
  <p>I understand why <code>__cxa_pure_virtual</code> is needed, but couldn't for the life of me get why I need a <code>delete</code> implementation.</p>
  
  <p>I perform no <code>new</code> or <code>delete</code> operations in the code, why would it be needed?</p>
  
  <p>When implementing both functions to satisfy the linker's demands, it seems that neither are called (as expected).</p>
  
  <p>Is there a way to avoid implementing these functions?</p>