I use GCC 5.2.0 to compile code for an EFM32 MCU (based on a Cortex-M core). I notice an awful increase in code size when I want to #include <iostream>
.
For example, let's compile the following code for an EFM32WG "Wonder Gecko" chip:
#include "em_device.h"
#include "em_chip.h"
#include <iostream>
int main(void)
{
CHIP_Init();
while (1) {
}
}
This code will result in 172048 bytes of code, whereas without #include <iostream>
it is only 1440 bytes.
I usually just use cout
for debug output (by implementing the _write
function for newlib and routing the output to the SWO pin), but it looks like this approach is very wasteful, considering the MCU only has 256k of flash, and just including this header will make the code use up most of it.
So, my question is: why is including the iostream header make the compiled code take such an insane amount of flash space? And also, is there a way to fix it?
EDIT:
Both the compiler and linker is arm-none-eabi-g++
(version 5.2.0), the C library is the nano C library (I think).
Here are my C++ compiler flags (excluding the include paths):
-g -gdwarf-2 -mcpu=cortex-m4 -mthumb '-DEFM32WG940F256=1' -O0 -Wall -c -fmessage-length=0 -mno-sched-prolog -fno-builtin -ffunction-sections -fdata-sections -mfpu=fpv4-sp-d16 -mfloat-abi=softfp
Here are my linker flags:
-g -gdwarf-2 -mcpu=cortex-m4 -mthumb -T "${BuildArtifactFileBaseName}.ld" --specs=nosys.specs -Xlinker --gc-sections -Xlinker -Map="${BuildArtifactFileBaseName}.map" -mfpu=fpv4-sp-d16 -mfloat-abi=softfp --specs=nano.specs
I tried both with and without optimalizations, but the resulting code size remains about the same (the optimized size is maybe 1k smaller).
EDIT 2
-fno-rtti
and -fno-exceptions
do not help with the code size either.