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.

Using pragmas 1:

Case-sensitivity must be respected.

Sample:

{warning 'This is not allowed'}
{attribute 'obsolete' := 'datatype FB_Sample not valid!'}

Possible insert positions

Using pragmas 2:

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:

Incorrect and correct positioning of a conditional pragma:

Incorrect:

Correct:

{IF defined(abc)}
IF x = abc THEN
{ELSE}
IF x = 12 THEN
{END_IF}
y := {IF defined(cde)} 12;
{ELSE} 13;
{END_IF}
END_IF
{IF defined(abc)}
IF x = abc THEN
{IF defined(cde)}
y := 12;
{ELSE}
y := 13;
{END_IF}
END_IF
{ELSE}
IF x = 12 THEN
{IF defined(cde)}
y := 12;
{ELSE}
y := 13;
{END_IF}
END_IF
{END_IF}
Using pragmas 3:

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:

Pragma categories

The TwinCAT pragmas are divided into the following categories:

See also: