iostream为什么要在MCU上占用这么多的闪存空间? [英] Why does iostream take so much flash space on an MCU?

查看:136
本文介绍了iostream为什么要在MCU上占用这么多的闪存空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCC 5.2.0编译EFM32 MCU(基于Cortex-M内核)的代码.我想#include <iostream>时,代码长度急剧增加.

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>.

例如,让我们为EFM32WG"Wonder Gecko"芯片编译以下代码:

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) {
  }
}

此代码将产生172048字节的代码,而没有#include <iostream>则只有1440字节.

This code will result in 172048 bytes of code, whereas without #include <iostream> it is only 1440 bytes.

我通常只使用cout进行调试输出(通过为newlib实现_write函数并将输出路由到SWO引脚),但是考虑到MCU只有256k的内存,这种方法看起来非常浪费. Flash,仅包含此标头将使代码用尽大部分.

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.

所以,我的问题是:为什么包含iostream标头会使编译后的代码占用如此疯狂的闪存空间?而且,有没有办法解决它?

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?

编译器和链接器都是arm-none-eabi-g++(版本5.2.0),C库是nano C库(我认为).

Both the compiler and linker is arm-none-eabi-g++ (version 5.2.0), the C library is the nano C library (I think).

这是我的C ++编译器标志(不包括include路径):

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

这是我的链接器标志:

-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

我尝试了优化和不优化,但是最终的代码大小保持不变(优化后的大小可能小了1k).

I tried both with and without optimalizations, but the resulting code size remains about the same (the optimized size is maybe 1k smaller).

编辑2

-fno-rtti-fno-exceptions也不有助于代码大小.

-fno-rtti and -fno-exceptions do not help with the code size either.

推荐答案

虽然编译器确实尝试消除了未使用的完整包含或部分包含,但这有时会失败.只是包含一些标头 导致代码运行-这意味着,即使您未引用标头中包含的任何内容,编译器也无法自由地从标头中删除代码.

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>是这样的示例,因为它声明了一些全局对象,这些对象的 构造函数在调用main之前运行.它的包含将大致 将STM32的二进制文件大小增加140kB.

<iostream> is such an example as it declares some global objects whose constructors are run before main is called. Its inclusion will roughly increase the binary size for an STM32 by 140kB.

您可以在解决方案是避免在微控制器上使用C语言提供的打印功能,例如printf().

The solution is to avoid on microcontrollers and use what C offers for printing such as printf().

这篇关于iostream为什么要在MCU上占用这么多的闪存空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆