Skip to main content

Using Pragmas

Pragma in CODESYS

A pragma is a text in the source code of the application that is enclosed in braces. 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).

Important

Uppercase and lowercase characters have to be respected.

Pragma categories

The CODESYS pragmas are divided into the following categories:

  • Message pragmas

    Custom messages can be displayed during the compile process.

  • Attribute pragmas

    Compile and precompile can be influenced. To do this, pragmas are available for different purposes, for example for initialization, monitoring. or adding a variable to the symbol configuration.

  • Conditional Pragmas

    Code generation can be controlled by conditional statements. For example, the behavior of a variable may be different under certain conditions.

  • Region pragma

  • Pragmas in test POUs

  • User-defined pragmas

Tip

In the Properties dialog (Compile category), you can declare compiler "defines" which can be queried in pragmas.

Possible insertion positions

Important

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

You can insert a pragma for the compiler to evaluate at various positions.

. In the declaration
  • In the declaration header of a POU

    You can enter the desired pragma directly in the text editor as a separate line, either as the first line of the POU or before a variable declaration.

    In the tabular view, you will get support when you enter the pragma. Click the Edit Declaration Header command. In the dialog, click the Attribute button and enter the desired pragma in the input field.

  • In a global variable list

Restriction in the case of conditional pragmas

Use pragmas for conditional compiling only in implementations of POUs. In declarations, these pragmas are ignored, not evaluated.

Note the following exception: The IF pragma with the project_defined operator can be inserted into declarations. For more information, see the following: IF pragma with project_defined (<global define>)

. In the implementation of a POU
  • Structured Text (ST)

    In the ST implementation language, a pragma belongs to a “statement position”. A valid position is at the beginning of the implementation on a separate line. Another valid position is labeled after a statement ending with a semicolon ";" or by keywords such as END_VAR, END_IF, or END_WHILE.

  • Function Block Diagram (FBD) or Ladder Diagram (LD)

    In the implementation languages of the FBD/LD/IL editor, a pragma belongs at a position which is also suitable for a label. You can enter the pragma just like a label. To do this, select the command FBD/LD/ILInsert Label. Then, in the text field of the label, replace the default text Label: with the desired pragma. To use a pragma in addition to a label, you specify the pragma first and then the label.

Example 17. Example

For incorrect and correct positions for a conditional pragma

PROGRAM PRG_ConditionalPragma
VAR
    strTest : STRING := 'b';
    iResult : INT := 0;
END_VAR

Incorrect

{IF defined(DEFINE_A)}
IF strTest = 'a' THEN
{ELSE}
IF strTest = 'b' THEN
{END_IF}
    iResult := {IF defined(DEFINE_B)} 12; {ELSE} 13; {END_IF}
END_IF

Correct

{IF defined(DEFINE_A)}
IF strTest = 'a' THEN
    {IF defined(DEFINE_B)}
    iResult := 16#A;
    {ELSE}
    iResult := 16#AA;
    {END_IF}
END_IF
{ELSE}
IF strTest = 'b' THEN
    {IF defined(DEFINE_B)}
    iResult := 16#B;
    {ELSE}
    iResult := 16#BB;
    {END_IF}
END_IF
{END_IF}


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.