The preprocessor is a powerful tool in the C language, but it can also lead to confusion and consequently to errors. Therefore, only a subset of the well-known preprocessor directives in C is offered in CAPL with comparable semantics.
#include: Include files contain arbitrary but complete sections of a CAPL program: includes, variables and procedures. In contrast to C, the text of an include file is not simply inserted into the CAPL file, rather the sections. All sections of the included file apply to the entire parent CAPL file "as if" they were contained in that file. The sequence of sections is irrelevant in CAPL anyways. This means the compiler reports any duplicate symbols as an error. Moreover, code and data from included and parent files may use each other mutually. One exception to the just stated prohibition of duplicate symbols is that on start, on preStart, on preStop and on stopMeasurement may coexist in both the included file and the parent file. In these functions, the code is executed sequentially: first the code from the included file and then the code from the parent file. This means that the Include files are used to perform three tasks: declare data types, define variables and provide an (inline) function library.
#pragma library: CAPL programs can use Windows DLLs created in other languages, as long as they implement a suitable CAPL DLL interface. These DLLs can be directly linked with the directive #pragma library("capldll.dll").
Macros: In CAPL, there are a number of predefined macros that are available to users for use in the code or for conditional compiling. Macros for use in the code can be used anywhere in the code without restriction. In contrast to C, macros may be used freely within string constants, identifiers for variables, and function names. They always begin and end with a % character, and they are primarily used to write generic programs.
Available code macros include the node name, index of the current channel, name of the current network and the type of bus being used. The code can access the name of the containing file with %FILE_NAME%, or it can access the name of the program file currently being compiled with %BASE_FILE_NAME%. In the case of Include files, the latter is the parent file.
Here are two simple examples:
write("The node name is %NODE_NAME%");
There is a separate set of predefined macros for the conditional compiling of code sections. They are #if, #else, #elif or #endif. Within a program, they allow distinguishing between the program types simulation node, measurement node and test program as well as the CANoe version that is used.
Here is an example that uses a #pragma message:
#if (TOOL_MAJOR_VERSION < 8)
#pragma message(“This program needs at least CANoe V 8 “)
#pragma message: The #pragma message directive lets users output their own message during the compiling process, e.g. the version number of the currently compiling CAPL program. It appears together with the other messages, warnings, errors, and general messages of the compiler.