易失性变量,用于制作应用程序 [英] volatile variable using in making application

查看:84
本文介绍了易失性变量,用于制作应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个领域的新手.以前,我在微控制器编程中使用易失性变量来避免编译器优化.但是在变量声明之前,我从未见过如此易变的声明.这是否意味着在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.

  1. 如何在编译过程中启用不同类型的优化,例如Angstrom构建中的速度和空间优化?

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

volatile通常不是用于优化代码,而是用于

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屋!

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