Using pragmas
Pragmas in TwinCAT
A pragma is text in the application source code that is enclosed in curly brackets. Pragmas are used to insert special instructions in the code that the compiler can evaluate. This allows a pragma to influence the properties of one or several variables with respect to precompilation or compilation (code generation). Pragmas unknown to the compiler are read over like a comment.
The instruction string of a pragma can also be multi-line. Please refer to the descriptions of the individual pragmas for details about the syntax.
There are pragmas for different effects: initialization of a variable, monitoring of a variable, forcing of message outputs during the compilation procedure, behavior of a variable under certain conditions, etc.
Case-sensitivity must be respected. |
Sample:
{warning 'This is not allowed'}
{attribute 'obsolete' := 'datatype FB_Sample not valid!'}
Possible insert positions
Pragmas in TwinCAT are not one-to-one implementations of the C preprocessor directives. A pragma has to be positioned like a normal instruction. A pragma cannot be used within an expression. |
You can insert a pragma to be evaluated by the compiler at the following positions:
- In the declaration part of a programming block:
- In the textual declaration editor, you enter pragmas directly as line(s), either at the beginning of the block or before a variable declaration.
- In the tabular editor, pragmas, which must be above the first declaration line, can be added in the Attributes dialog. Double click on the column Attributes.
- In a global variable list
- In the implementation part of a programming block:
- The pragma must be placed at a "instruction position", i.e. at the beginning of a programming block in a separate line, or after a ";" or END_IF, END_WHILE etc..
- FBD/LD/IL editor: enter pragmas in networks of the FBD/LD/IL editor like a label:
To do this, select the command FBD/LD/IL > Insert label and then replace the standard text Label: in the label text field with the corresponding pragma instruction. If you want to use a pragma in addition to a label, first enter the pragma, then the label.
Incorrect and correct positioning of a conditional pragma:
Incorrect: | Correct: |
|
|
In the POU Properties, category Advanced, you can enter Defines, which can be queried in pragmas. |
Scope:
Depending on the type and content of a pragma, it affects the following:
- the following declarations
- specifically the following instruction
- all following instructions until it is canceled with a corresponding pragma.
- all following instructions, until the same pragma is executed with other parameters or the end of the code is reached. "Code" in this context means: declaration part, implementation part, global variable list, type declaration. A pragma, which appears on its own in the first line of the declaration part and is not superseded or canceled by another pragma, is therefore active for the entire object.
Pragma categories
The TwinCAT pragmas are divided into the following categories:
- Attribute pragmas: influence compilation and precompilation (Attribute pragmas)
- Message pragmas: output of user-defined messages during the compile process (Message pragmas)
- Conditional pragmas: influence the code generation (Conditional pragmas)
- User-defined pragmas (User-defined attributes)
See also: