问题描述
大家好,
我们正在研究CrossCore Tool,我们开发了一个嵌入式C ++应用程序。
我们的二进制大小很大,但我们需要减小二进制大小以适应不同的MCU!
C ++中是否有任何技术可以帮助我们减少二进制大小?
我们使用的编译器是GCC。
请在这方面帮助我们!
我们已经尝试了
内联函数,虚函数,未使用的变量删除和将局部变量移动到最内层范围
但这些技术帮助我们提高了性能而不是尺寸,我们需要减小尺寸!
问候,
Raja Vignesh
我有什么尝试过:
大家好,
我们试过了
- >内联函数
- >虚拟功能
- >未使用的变量删除
- >将局部变量移动到最大范围内
但是这些技术帮助我们提高了性能而不是尺寸,我们需要减小尺寸!
错误:由于需要较少的堆栈操作,内联函数显式增加代码大小以获得性能。
最多没有影响,实际上由于虚拟表需要空间,它会稍微增加代码大小。
除非它们是静态的,否则它不会改变代码大小,而是在运行时更改应用程序的内存占用量。很好,但不适合你想要达到的目标。
对于什么?变量声明不会增加代码大小,只增加内存占用量。在函数内部,所有变量都存储在堆栈中。充其量,使用非常专业的编译器,您可以获得堆栈地址重用,以最大限度地减少运行时内存占用。
为了最小化代码大小,您应该:
*消除所有不需要的静态库。
*减少层次结构中的调用路径和中间对象的数量。
*如果可能,使用具有多个签名的函数,只使用其中的一个按顺序在编译时没有链接其他变体。小心:单个遗漏函数会将其变体添加到链接文件中。
*查看您的跨平台框架是否具有选项(通常采用#defines形式)以消除大多数编组和中间调用针对特定MCU编译。跨平台框架很棒,但它们确实增加了代码大小。
*消除像boost这样的大型库,这很棒但很臃肿。
这些是我的2美分,
Denis
Hi all,
We are working on CrossCore Tool, on which we have developed a embedded C++ application.
Our binary size is big, but we need to reduce the binary size so as to fit in different MCU!
Is there any techniques in C++ that really help us in reducing binary size?
The compiler we use is GCC.
Please help us in this regard!
We have tried
Inline Functions, Virtual Functions, Unused variables removal and Moving local variables to the inner most scope
But these techniques have helped us in improving performance rather than size, we need size reduction!
Regards,
Raja Vignesh
What I have tried:
Hi all,
We have tried
-> Inline Functions
-> Virtual Functions
-> Unused variables removal
-> Moving local variables to the inner most scope
But these techniques have helped us in improving performance rather than size, we need size reduction!
Bad: inline functions explicitly increase code size to the benefit of performances due to less stack manipulation required.
No impact at best, actually it increases the code size a bit due to the virtual tables needing space.
Unless they are statics it does not change code size but the memory footprint of the application when running. Good but not for what you want to achieve.
What for? A variable declaration does not increase code size, only memory footprint. Inside a function ALL the variables are memorized in the stack. At best, with very specialized compilers, you earn a stack address reuse to minimize runtime memory footprint.
To minimize code size you should:
* Eliminate all unneeded static libraries.
* Reduce the call paths and the number of intermediate objects in the hierarchy.
* If possible, with functions having multiple signatures, using only a single one of them in order to not have the other variants linked at compile time. Careful: a single missed function will add its variant to the linked file.
* See if your cross platform framework has options (usually in form of #defines) to eliminate most of th marshalling and intermediate calls when compiled for a specific MCU. Cross platform frameworks are great but they do increase code size.
* Eliminate any huge library like boost, which is fantastic but bloated.
These are my 2 cents,
Denis
这篇关于C ++中大小的代码优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!