GCC: How to disable heap usage entirely on an MCU?
<p>I have an application that runs on an ARM Cortex-M based MCU and is written in C and C++. I use <code>gcc</code> and <code>g++</code> to compile it and would like to completely disable any heap usage.</p>

<p>In the MCU startup file the heap size is already set to 0. In addition to that, I would also like to disallow any accidental heap use in the code.</p>

<p>In other words, I would like the linker (and/or the compiler) to give me an error when the <code>malloc</code>, <code>calloc</code>, <code>free</code> functions or the <code>new</code>, <code>new[]</code>, <code>delete</code>, <code>delete[]</code> operators are used.</p>

<p>So far I've tried <code>-nostdlib</code> which gives me issues like <code>undefined reference to _start</code>. I also tried <code>-nodefaultlibs</code> but that one still does not complain when I try to call <code>malloc</code>. What is the right way to do this?</p>

<p>Notes:</p>

<ul>
<li>This app runs on “bare metal”, there is no operating system.</li>
<li>I would also like to avoid any malloc usage in 3rd-party code (vendor-specific libraries, the standard library, printf etc.).</li>
<li>I'm fully okay with not using the parts of the C / C++ standard libraries that would require dynamic memory allocations.</li>
<li>I'd prefer a compile-time rather than a run-time solution.</li>
</ul>