如何停止优化器丢弃未使用的变量?

How to stop optimizer discard unused variables?

我想调试我的代码,但无法访问进程中的内部层,因为这会干扰与硬件的通信。 (在监视列表会干扰基本读取访问的情况下执行易变操作。)

所以我正在测试接口的 return 值,但 IAR 编译器甚至优化了未使用的 volatile 变量。

和这样的声明:

i = object.foo();
if (i)i=i;

也无济于事。

我在 SO 上发现这里只是回答了针对该案例使用 i/o 操作的建议。但这也不是一个选择,因为我没有包含 C 标准库的选项。并且项目本身不需要 i/o input/output 函数没有自己的变体。

除了禁用优化器,我还有哪些选择?

一种常见的方法是通过宏将其放入while循环中。

#define KEEP_UNUSED(var) do { (void)var; } while(false);

IAR 还有一个名为 __root 的扩展: http://supp.iar.com/FilesPublic/UPDINFO/004350/manuals.htm

The __root attribute can be used on either a function or a variable to ensure that, when the module containing the function or variable is linked, the function or variable is also included, whether or not it is referenced by the rest of the program.

最可靠的方法是在您的链接器文件中找到一个强制链接特定变量的设置。但这当然完全取决于系统。

否则,可移植的标准解决方案就是在代码的某处写上(void)i;。这适用于大多数编译器。如果没有,您可以更进一步:

#ifdef DEBUG_BUILD
volatile int dummy;

// all variables you would like to have linked:
dummy = i; 
dummy = j;
...
#endif

或者如果您喜欢晦涩难懂的宏:

#define FORCE_LINKING(x) { void* volatile dummy = &x; }

(void* 因为它是泛型类型,适用于各种变量。* volatile 使指针本身易变,这意味着禁止编译器优化写入它。)