Using Pragmas

Pragma in CODESYS

A pragma is a text in the source code of the application that is enclosed in curly brackets. Pragmas are used to insert special statements in the code, which the compiler can evaluate. This allows a pragma to influence the properties of one or more variables with respect to precompilation or compilation (code generation). Pragmas that the compiler does not recognize are passed over as a comment.

The statement string of a pragma can also extend over multiple lines. For more details about the syntax, see the descriptions of the individual CODESYS pragmas.

There are different pragmas for different purposes (example: initialization of a variable, monitoring of a variable, adding a variable to the symbol configuration, forcing the display of messages during the compilation process, and behavior of a variable under certain conditions).

Hint

Uppercase and lowercase characters have to be respected.

Examples

{warning 'This is not allowed'}

{attribute 'obsolete' := 'datatype fb1 not valid!'}

{attribute 'Test':='TestValue1;
                    TestValue2;
                    TestValue3'}

Possible insertion positions

Hint

Pragmas in CODESYS are not one-to-one implementations of C preprocessor directives. You have to position a pragma like an ordinary statement. You must not use a pragma within an expression.

A pragma that the CODESYS compiler should evaluate can be inserted at the following positions:

  • In the declaration part of a POU:
    • In the textual declaration editor, specify pragmas directly as line(s), either at the beginning of the POU or before a variable declaration.
    • In the tabular editor, you specify pragmas that should be located before the first declaration line in the Edit Declaration Part / Attributes dialog.
  • In a global variable list
  • In the implementation part of a POU:
    • The pragma has to be at a “statement position”, meaning at the beginning of a POU on a separate line, or after a “;” or END_IF, END_WHILE, etc.
    • FBD/LD/IL editor: In networks of the FBD/LD/IL editor, you insert pragmas like a label by means of the FBD/LD/IL ‣ Insert Label command. Then, in the text field of the label with the corresponding pragma statement, replace the default text Label:. To use a pragma in addition to a label, you specify the pragma first and then the label.

Incorrect and correct positions for a conditional pragma

INCORRECT:

{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

CORRECT:

{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}

Note

In the Properties dialog (Compile category), you can specify “defines” that can be queried in pragmas.

Scope:

Depending on the type and contents of a pragma, it may influence the following:

  • Subsequent declarations
  • Exactly the next statement
  • All subsequent statements until it is canceled by a corresponding pragma
  • All subsequent statements until the same pragma is executed with other parameters or the end of the code is reached. In this context, “code” means the declaration part, implementation part, global variable list, and type declaration. Therefore, a pragma influences the entire object when the pragma is alone on the first line of the declaration part and is not superseded or canceled by another pragma.

Pragma categories in CODESYS

The CODESYS pragmas are divided into the following categories:

  • Attribute pragmas (influence compiling and precompiling)
  • Message pragmas (print user-defined messages when compiling)
  • Conditional pragmas (influence code generation)
  • User-defined pragmas

See also