当前位置:网站首页>What are strong and weak symbols in embedded systems?
What are strong and weak symbols in embedded systems?
2022-06-22 14:53:00 【Lixiaoyao】
Focus on 、 Official account of star standard , Straight to the highlights
source : Online material
__attribute__ It's a compiler instruction , It's actually GNU C A mechanism of , The essence is a compiler instruction , You can provide some attributes when declaring , Works at compile time , To do diversified error checking and advanced optimization .
Used in C、C++、Objective-C Modify variables in 、 function 、 Parameters 、 Method 、 Class etc. .
The rational use of __attribute__ What are the benefits ?
Give the compiler context , Help compiler optimize , Reasonable use can achieve significant optimization effect . The compiler will respond by __attribute__ Generate some compilation warnings , Make the code more standard . Provide necessary comments to code readers , Help them understand the intent of the code .
All in all , __attribute__ It serves as a context for the compiler , If the wrong use __attribute__ Instructions , Because the compiler is given the wrong context , The resulting errors are often difficult to detect .
Strong sign and weak sign
You cannot define the same variable or function under the same scope , quite a lot C Language learners take it for granted that .
This is actually biased ,GNU C To the standard C The language has been extended , stay GCC in , For symbols ( At compile time , Variables and functions are abstracted into symbols ) for , There are strong signs and weak signs . Yes , Whether this feature is supported depends on C Language standards .
about C/C++ for , Compiler default functions and initialized global variables are strong symbols , Uninitialized global variables are weakly signed , When the programmer does not display the specified , The compiler has some default behavior for the definition of strong and weak symbols , At the same time, developers can also specify symbols , Use "attribute((weak))" To declare a symbol as weak .
Define the same variable , When both are not strong symbols ,gcc No error will be reported during compilation , But follow certain rules to choose :
When both are strong signs , Report errors :redefinition of 'xxx'
When the two are strong and weak , Pick the value of the strong sign
When both are weak , Select the symbol that takes up more space , This is actually a good understanding , The compiler doesn't know what the programmer intended , Choosing symbols that take up a lot of space will at least not cause problems such as overflow 、 Cross the border and other serious consequences .
In the case of the default symbol type , Strong and weak symbols can coexist , Something like this :
int x;
int x = 1;Compile without error , At compile time x The value of will be 1.
But use __attribute__((weak)) Convert strong symbols to weak symbols , But cannot coexist with a strong symbol , Something like this :
int __attribute__((weak)) x = 0;
int x = 1;The compiler will report a duplicate definition error .
Strong quotes and weak quotes
Apart from the difference between strong and weak symbols ,GNUC Another feature is strong and weak references , What we know is , The compiler is only responsible for compiling the source file into the target file in the compilation phase ( Binary file ), Then the linker links all binary files .
In separate compilation , When the compiler checks that the currently used function or variable is only declared but not defined in this module , The compiler uses this symbol directly , Transfer work to linker , The linker is responsible for finding the entity address of these functions or variables based on this information .
Because during the execution of the program , The program must know exactly the address of each function and global variable . If the entity of the symbol is not found , Will report undefined reference error , Such references between symbols are called strong references .
The compiler defaults to strong references for all variables and functions , At the same time, programmers can use __attribute__((weakref)) To declare a function , Note that this is a declaration, not a definition , Since it's a citation , Then it is to use the entities defined in other modules , For functions , We can use this expression :
__attribute__((weakref)) void func(void);And then call it in the function. func(), If func() Not defined , be func The value of is 0, If func Defined , Then call the corresponding func, stay 《 Self cultivation of programmers 》 This book introduces , It's written like this :
__attribute__((weakref)) void func(void);
void main(void)
{
if(func) {func();}
}But in modern compilation systems , This is wrong , Although the compilation passed ( There are warning messages ), But it's not true :
warning: ‘weakref’ attribute should be accompanied with an ‘alias’ attribute [-Wattributes]The warning shows :weakref It needs to be accompanied by an alias to work properly
strong / Weak sign and strong / The role of weak references
This weak sign 、 The extension mechanism of weak references is very useful in the implementation of libraries . We can use weak notation and weak reference mechanism in the library , So for a weakly signed function , Users can customize the function that extends the function to cover the weak symbolic function .
At the same time, we can define some extended function functions as weak references , When users need to use extended functions , Define it , Link to the program , If the user does not define , Then the link will not report an error , This makes it easy to cut and combine the functions of the library .
Be careful :C There is no mention of strong in the standard 、 Weak sign . This is just GCC This implementation defines the features , stay MS C There is no such concept in the compiler .
Reference blog :
https://www.cnblogs.com/downey-blog/p/10470674.html https://blog.csdn.net/astrotycoon/article/details/8008629
link :https://xiaoyege.blog.csdn.net/article/details/93674095
Copyright notice : Source network of this paper , Free delivery of knowledge , The copyright belongs to the original author . If involves the work copyright question , Please contact me to delete .
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
Pay attention to my WeChat official account , reply “ Add group ” Join the technical exchange group according to the rules . Click on “ Read the original ” See more sharing , Welcome to share 、 Collection 、 give the thumbs-up 、 Looking at .边栏推荐
- Using pictures to locate app elements sikulix
- Excuse me, is Zhongyan futures reliable? Is the fund safe?
- Differences and performance of array, list, ArrayList, directory and LinkedList in C #
- Fight, programmer -- Chapter 42 will draw a bow like a full moon, look northwest and shoot Sirius
- Redistemplate serialization
- 请问,中衍期货是不是靠谱的?资金安全吗?
- Struggle, programmer -- Chapter 47 the so-called Iraqis are on the water side
- 11 method reference and constructor application
- Sikulix select the picture of relative position (advanced version)
- 世界上所有的知名网络平台
猜你喜欢

网络地址转换NAT

Specific methods and steps of PROFINET communication between s7-200smart and Fanuc robot

RealNetworks vs. Microsoft: the battle in the early streaming media industry
![[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide

难怪考研热度这么高,这些是研究生才有的“隐藏福利”!

NF-ResNet:去掉BN归一化,值得细读的网络信号分析 | ICLR 2021

PHP built-in protocols (supported and encapsulated protocols)

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-5

扩散模型又杀疯了!这一次被攻占的领域是...

How location coding (PE) works in transformers
随机推荐
世界上所有的知名网络平台
visual studio开发过程中常见操作
Software architecture
Excuse me, is Zhongyan futures reliable? Is the fund safe?
Sikulix选取相对位置的图片(进阶版)
Struggle, programmer -- Chapter 44: eight hundred miles under one's command, fifty strings turning over the Great Wall
本周四晚19:00战码先锋第7期直播丨三方应用开发者如何为开源做贡献
U++ 运营商 学习笔记
Error: unable to find a match: lrzsz
phpStudy 2016搭建-pikachu靶场
Chengdu test equipment development_ Array introduction of C language for single chip microcomputer
Thoroughly understand the factory mode
个人免签支付方案推荐
Lisez ceci pour vous apprendre à jouer avec la cible de test de pénétration vulnhub - driftingblues - 5
Mysql学习笔记2022
C# Winform 相册功能,图片缩放,拖拽,预览图分页
bochs 软件使用记录
看完這篇 教你玩轉滲透測試靶機Vulnhub——DriftingBlues-5
C语言学生管理系统(开源)
Go all out to implement the flood control and disaster relief measures in detail and in place, and resolutely protect the safety of people's lives and property
