易失性变量,用于制作应用程序 [英] volatile variable using in making application
问题描述
我是这个领域的新手.以前,我在微控制器编程中使用易失性变量来避免编译器优化.但是在变量声明之前,我从未见过如此易变的声明.这是否意味着在Arago构建中无需进行任何优化即可完成编译.在这里,我有两个问题.
I am new in this field. Previously I was doing microcontroller programming where I used volatile variables to avoid compiler optimization. But I never saw such volatile declaration before variable declaration. Does it mean compilation is done without any optimization in Arago build. Here I have two questions.
-
如何在编译过程中启用不同类型的优化,例如Angstrom构建中的速度和空间优化?
How can I enable different types of optimization during compilation like speed and space optimization in Angstrom build?
如果它已经是优化的编译了,为什么我们不需要volatile声明?
If it is already an optimized compilation, why do we not need volatile declarations?
推荐答案
如果您使用gcc
进行编译,则添加/修改CFLAGS
If you are using gcc
for compilation then add/modify CFLAGS
-
-O2
或-O3
可以实现一系列通用性能优化.
-O2
or-O3
to enable a bunch of generic performance optimisations.
Os
启用代码大小优化.
可以使用一整套控制单个gcc编译器优化选项的标志 这里 .
A long list of flags that control individual gcc compiler optimisation options is available here.
Most often volatile
is used NOT for optimising the code, but to ensure validity of data.
将变量声明为volatile
告诉编译器,可以在实现外部随时通过以下方式修改变量:
The declaration of a variable as volatile
tells the compiler that the variable can be modified at any time externally to the implementation by
- 操作系统
- 另一个执行线程
-中断程序
-信号处理程序 - 底层硬件
- the operating system
- another thread of execution
-- interrupt routine
-- signal handler - underlying hardware
由于挥发性限定变量的值可以随时更改,因此只要在代码中引用该变量,就必须始终访问实际变量.
As the value of a volatile-qualified variable can change at any time, the actual variable must always be accessed whenever the variable is referenced in code.
这意味着编译器无法对变量执行优化.标记变量volatile
会强制编译器生成代码,该代码将忽略CPU寄存器中的变量,并实际读取映射到该变量所指向的地址的基础内存/硬件寄存器.
This means the compiler cannot perform optimizations on the variable. Marking a variable volatile
forces the compiler to generate code that ignores the variable in the CPU register and actually reads the underlying memory/hardware-register mapped at the address referred-to by the variable.
还要检查使用 volatile
以及编译器优化的各个方面 > .
Also checkout the various aspects of using volatile
along-with compiler optimisations.
这篇关于易失性变量,用于制作应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!