属性'const_replaced' 的作用是在代码中替换常量,与“替换常量” 编译器选项的设置无关。该属性只对标量类型的变量有效,对数组和结构体等复合类型的变量无效。
您可以相应地插入 pragma{attribute 'const_non_replaced'} ,以明确停用“替换常量” 编译器选项。例如,在符号配置中,尽管有编译器选项,但常量是可用的,可以导出。
“Project Settings” 对话框的“Compile Options” 类别中的“Replace constants” 选项是为整个项目预设的,因为替换常量通常会加快代码速度并减少内存使用。
语法 :
{attribute 'const_replaced'}
{attribute 'const_non_replaced'}
插入位置:全局变量声明行的上方一行。
示例
在符号配置中,常量iTestCon 和xTestCon 可用,因为“替换常量” 选项已停用。
{attribute 'qualified_only'}
VAR_GLOBAL CONSTANT
{attribute 'const_non_replaced'}
iTestCon : INT := 12;
{attribute 'const_non_replaced'}
xTestCon : BOOL := TRUE;
rTestCon : REAL := 1.5;
END_VAR
VAR_GLOBAL
iTestVar : INT := 12;
xTestVar : BOOL := TRUE;
END_VAR