Using Pragmas

What is a pragma in CODESYS?

A pragma is a special statement in the source code of an application that influences the properties of variables when precompiling or compiling (code generation).

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 must be maintained

How and where to insert a pragma?

A pragma statement is specified in braces. Attributes and texts used within the statement are enclosed in single straight quotation marks. The opening brace may follow immediately after a variable name. Opening and closing braces must be located on the same line.

Examples

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

Possible insertion points:

  • Declaration part of a POU

    In the textual declaration editor, you specify pragmas directly as a line. In the tabular editor, you specify pragmas that must be located before the first declaration line in the dialog box Edit declaration part / Attributes.

  • Global variable list

  • Implementation part of a POU (on a separate line or on a line with code text)

    FBD/LD/IL: Pragmas in networks of the FBD/LD/IL editor are entered as a label. Click the command FBD/LD/IL ‣ Add label and replace the default text Label: in the text field with the appropriate pragma statement. To use a pragma in addition to a label, you specify the pragma first and then the label.

Hint

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

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 box (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
  • the next statement only
  • 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.

What kinds of pragmas are there?

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