简体   繁体   English

为什么 iostream 在 MCU 上占用如此多的闪存空间?

[英]Why does iostream take so much flash space on an MCU?

I use GCC 5.2.0 to compile code for an EFM32 MCU (based on a Cortex-M core).我使用 GCC 5.2.0 为 EFM32 MCU(基于 Cortex-M 内核)编译代码。 I notice an awful increase in code size when I want to #include <iostream> .当我想#include <iostream>时,我注意到代码大小急剧增加。

For example, let's compile the following code for an EFM32WG "Wonder Gecko" chip:例如,让我们为 EFM32WG “Wonder Gecko”芯片编译以下代码:

#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.此代码将产生 172048 字节的代码,而没有#include <iostream>则只有 1440 字节。

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.我通常只使用cout进行调试输出(通过为 newlib 实现_write函数并将输出路由到 SWO 引脚),但看起来这种方法非常浪费,考虑到 MCU 只有 256k 的闪存,并且只包含这个头将使代码使用大部分。

So, my question is: why is including the iostream header make the compiled code take such an insane amount of flash space?所以,我的问题是:为什么包含 iostream 头文件会使编译后的代码占用如此多的闪存空间? 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).编译器和链接器都是arm-none-eabi-g++ (版本 5.2.0),C 库是 nano C 库(我认为)。

Here are my C++ compiler flags (excluding the include paths):这是我的 C++ 编译器标志(不包括包含路径):

-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).我尝试了优化和不优化,但结果代码大小保持不变(优化大小可能小 1k)。

EDIT 2编辑 2

-fno-rtti and -fno-exceptions do not help with the code size either. -fno-rtti-fno-exceptions对代码大小也没有帮助。

While the compiler does try to eliminate complete includes or parts of them that are not used this sometimes fails.虽然编译器确实尝试消除完整的包含或其中未使用的部分,但这有时会失败。 Some headers just by being included cause code to be run - meaning that even if you do not refer to anything included from the header the compiler is not free to remove the code from it.一些头文件仅仅因为被包含而导致代码运行——这意味着即使你不引用头文件中包含的任何内容,编译器也不能自由地从中删除代码。

<iostream> is such an example as it declares some global objects whose constructors are run before main is called. <iostream>就是这样一个例子,因为它声明了一些全局对象,其构造函数在调用 main 之前运行。 Its inclusion will roughly increase the binary size for an STM32 by 140kB.包含它会使 STM32 的二进制大小大致增加 140kB。

You can check this behaviour and reasoning of the gcc developers on github .您可以在github上检查 gcc 开发人员的这种行为和推理。

The solution is to avoid on microcontrollers and use what C offers for printing such as printf() .解决方案是避免在微控制器上使用 C 提供的打印功能,例如printf()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM