简体   繁体   English

STM32链接器脚本初始化部分,使用C时需要吗?

[英]STM32 linkerscript initialization sections, are they needed when using C?

As far as I know, sections like.init, .preinit_array, .init_array, .finit, .fini_array... found in STM32CubeIDE linkerscripts are used in C++ for calling the static objects' constructors that need to be executed before main (and the fini versions for the destructors).据我所知,STM32CubeIDE链接器脚本中的.init、.preinit_array、.init_array、.finit、.fini_array ...等部分在C++中用于调用static对象的构造函数之前需要执行析构函数的fini版本)。

My assumption is that these sections are used by functions called implicitly by the compiler and the C/C++ runtime libraries, but if your firmware is written in C, are all these sections really needed?我的假设是这些部分由编译器和 C/C++ 运行时库隐式调用的函数使用,但是如果您的固件是用 C 编写的,那么真的需要所有这些部分吗? What does the compiler do behind the scenes?编译器在幕后做了什么?

You can live without many of them.你可以没有他们中的许多人。

Other than C++, some of them may initialize things required by the standard library.除了 C++ 之外,其中一些可能会初始化标准库所需的东西。 If you only call pure functions from the standard library and you only have code in C or assembly then you could try taking them out.如果您只从标准库中调用函数并且您只有 C 或程序集中的代码,那么您可以尝试将它们取出。

If you are trying to do this as a learning exercise, then take them out and just see what doesn't work.如果您尝试将此作为学习练习,那么将它们拿出来,看看什么不起作用。 Also search on google, there are loads of sites that explain this sort of thing in a way that is far too long to reproduce here.也在谷歌上搜索,有很多网站以一种太长方式来解释这类事情,无法在这里重现。

If you are just trying to get your project finished, then leave them alone.如果你只是想完成你的项目,那就别管他们了。 They only add a tiny amount to your program size and it isn't worth your time to fight with them.它们只会增加您的程序大小的一小部分,不值得您花时间与它们抗争。

are used in C++ for calling the static objects' constructors that need to be executed before main (and the fini versions for the destructors).在 C++ 中用于调用需要在 main 之前执行的 static 对象的构造函数(以及析构函数的 fini 版本)。

It is not 100% truth.这不是100%的真理。 cubeIDE uses gcc based ARM toolchain which has some extensions which may use some of those sections. cubeIDE 使用基于 gcc 的 ARM 工具链,它有一些扩展,可能会使用其中一些部分。 For example, you can use use attributes to make functions which will be executed before main and/or called after the main function return.例如,您可以使用 use 属性来创建将在main之前执行和/或在main function 返回之后调用的函数。

void __attribute__((constructor)) called_before_main(void)
{
   /* some code */
}

void __attribute__((destructor)) called_after_main(void)
{
   /* some code */
}

If you even not use any of those, external libraries may use them.如果您甚至不使用其中任何一个,外部库也可能会使用它们。 Even if you do not use external libraries keeping those sections does not hurt as they will be discarded if they are empty.即使您不使用外部库,保留这些部分也不会受到伤害,因为如果它们为空,它们将被丢弃。

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

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