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中定义或者放在主文件中定义。