22.3 Pragma Directives

There are certain compile-time directives that can be used to modify the behavior of the compiler. These are implemented through the use of the ANSI standard #pragma facility. Any pragma which is not understood by the compiler will be ignored.

The format of a pragma is:
#pragma keyword options
where keyword is one of a set of keywords, some of which are followed by certain options. A description of the keywords is given below.

#pragma config

The #pragma config directive specifies the processor-specific configuration settings (that is, Configuration bits) to be used by the application. See 7.3 Configuration Bit Access.

default_function_attributes

The default_function_attributes = [@ "section"] pragma sets default section placement attributes for function declarations and definitions that do not otherwise specify this attribute. All functions after the pragma will be placed in the section whose name is quoted after the @ token. Using the default_function_attributes = form of this pragma will reset the section specification, so that subsequent functions are not placed in any special section.

default_variable_attributes

The #pragma default_variable_attributes = [@ "section"] pragma sets default section placement attributes for variable declarations and definitions that do not otherwise specify this attribute. All variables after the pragma will be placed in the section whose name is quoted after the @ token. Using the #pragma default_variable_attributes = form of this pragma will reset the section specification, so that subsequent variables are not placed in any special section.

#pragma interrupt

Mark a function as an interrupt handler. The prologue and epilogue code for the function will perform more extensive context preservation. Note that the interrupt attribute (rather than this pragma) is the recommended mechanism for marking a function as an interrupt handler. The interrupt pragma is provided for compatibility with other compilers. See 17 Interrupts and 17.4 Exception Handlers.

#pragma message

The message = "text" pragma prints the quoted text at build time, as neither a warning nor error, but purely as an advisory.

#pragma GCC optimize

The #pragma GCC optimize ("string"...) pragma sets default optimization attributes for function declarations and definitions that do not otherwise specify these attributes. All functions after the pragma will be optimized accordingly. The parentheses are optional. The arguments allowed may be:
  • A number n, to be interpreted as an optimization level, i.e., the -On option.
  • A string beginning with O, which is interpreted as an optimization option, i.e., -Ostring.
  • Otherwise, string should be an option that can be used with a -f prefix.
The #pragma GCC reset_options pragma clears the default optimizations, so that the optimization of subsequent functions is not controlled by optimize pragma.

#pragma pack

The #pragma pack pragma allows the maximum alignment of members of structures (other than zero-width bit-fields), unions, and classes to be changed. All aggregate objects after the pragma will be packed accordingly. subsequently defined.

The #pragma pack(n) form of this pragma specifies the new packing alignment. The n value should be a small power of two and specifies the new packing alignment in bytes.

The #pragma pack() form of this pragma resets the packing alignment to that in effect when compilation started. This might have been specified with the -fpack-struct option.

The #pragma pack(push[,n]) form of this pragma pushes the current alignment setting on an internal stack and then optionally sets the new packing alignment to the value n.

The #pragma pack(pop) form of this pragma restores the alignment setting to the one saved at the top of the internal stack by a previous #pragma pack(push) (and removes that stack entry).

#pragma vector

Generate a branch instruction at the indicated exception vector that targets the function. Note that the vector attribute (rather than this pragma) is the recommended mechanism for generating an exception/interrupt vector. See 17 Interrupts and 17.4 Exception Handlers.

#pragma weak

The #pragma weak pragma can be used to declare weak symbols and defining weak aliases.

The #pragma weak symbol form of this pragma declares symbol to be weak, as if the declaration had used the weak attribute. The pragma can appear before or after the declaration of symbol, or can be used in situations where symbol is never defined.

The #pragma weak symbol1 = symbol2 form of this pragma declares symbol1 to be a weak alias of symbol2. The symbol2 symbol must be defined in the current translation unit.