CANoe-CAPL-#define

CAPL编程中,#define是在includes中定义还是在variables中定义,决定了它的使用范围;是否被include,决定了是使用范围和是否生效。
1)在variables中定义的#define。从variables位置开始,对本文件后续所有部分生效,在被include的子文件中不生效。被include的文件,在variables中定义的#define可以在主文件生效。
2)在includes中定义的#define。如果此文件作为主文件,那么此#define对主文件include位置开始后面的所有部分,以及主文件include的子文件都生效。如果此文件被include,那么#define将不生效。

一个子文件,子文件的includes中定义了#define,在子文件的代码中无法识别定义的#define,导致子文件里面的相关代码无法执行。

分析:子文件中includes中定义的#define未执行,是无效的。查看help帮助文档:
In contrast to C/C++, #define is no macro definition. It is not possible to assign a value to the constant or to use the constant outside #if/#elif.
A #define in a variables section is valid from its position on for the rest of the file. If it is located in an included file, it is valid for the included file as of the #include instruction.
A #define in an includes section is taken into account only in a main file; it will be ignored in an included file. It is also valid from its position on for the rest of the file as well as for included files after the #define.
A #define in other functions or handlers are only applicable for the function itself.
There is no #undef or a similar type to remove a constant.
You also cannot exclude function declarations from the compile. #if can only be used within functions.
在这里插入图片描述
在这里插入图片描述
Include #define:表示在Includes中定义#define;
Variables #define:表示在Variables中定义#define;
绿色范围表示#define的生效范围。
分别创建两个.can文件进行验证:
Test1.can作为主文件,Test2.can作为子文件。
在Test1.can中,在includes和variables中分别创建#define JY_TEST1和JY_TEST2;
在Test2.can中,在includes和variables中分别创建#define JY_TEST3和JY_TEST4;
在on start中引用定义的#define,通过write输出信息进行调试。
在这里插入图片描述

在这里插入图片描述
子文件中includes中定义的#define是无效的,引用#define时无法执行,应该放在variables中定义或者放在主文件中定义。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜉蚁撼树

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值