简体   繁体   English

如何使用编译器的命令行选项禁用易失性存储类?

[英]How do I disable the volatile storage class using command-line options to the compiler?

Is there any way to disable the volatile storage class using only compiler settings/flags while compiling in gcc without modifying the underlying source code ? 有什么方法可以在gcc中编译而不修改基础源代码时仅使用编译器设置/标志来禁用易失性存储类?

Ex. 例如

volatile int x;
.. use x ..

Needs to be compiled as if it were written: 需要像编写的那样进行编译:

int x;
.. use x ..

Compile using 编译使用

gcc -Dvolatile="" ...

so that the preprocessor will replace each occurrence of volatile with an empty string. 这样预处理器将用空字符串替换每次出现的volatile If you were to just use -Dvolatile , volatile would be replaced with 1, which would cause compilation errors. 如果仅使用-Dvolatile ,则volatile将被替换为1,这将导致编译错误。

Because volatile keyword tells the compiler that the value may change at any point and that it should never cache the value, omitting them from working code will likely cause bugs to appear (as the compiler will sometimes work on stale values). 因为volatile关键字告诉编译器该值可能随时更改,并且永远不要缓存该值,因此从工作代码中忽略它们可能会导致错误的出现(因为编译器有时会使用过时的值)。

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

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