Next: , Up: Pragmas   [Contents][Index]

30.1.1 Pragma Basics

C defines two syntactical forms for pragmas, the line form and the token form. You can write any pragma in either form, with the same meaning.

The line form is a line in the source code, like this:

#pragma line

The line pragma has no effect on the parsing of the lines around it. This form has the drawback that it can’t be generated by a macro expansion.

The token form is a series of tokens; it can appear anywhere in the program between the other tokens.

_Pragma (stringconstant)

The pragma has no effect on the syntax of the tokens that surround it; thus, here’s a pragma in the middle of an if statement:

if _Pragma ("hello") (x > 1)

However, that’s an unclear thing to do; for the sake of understandability, it is better to put a pragma on a line by itself and not embedded in the middle of another construct.

Both forms of pragma have a textual argument. In a line pragma, the text is the rest of the line. The textual argument to _Pragma uses the same syntax as a C string constant: surround the text with two ‘"’ characters, and add a backslash before each ‘"’ or ‘\’ character in it.

With either syntax, the textual argument specifies what to do. It begins with one or several words that specify the operation. If the compiler does not recognize them, it ignores the pragma.

Here are the pragma operations supported in GNU C.

#pragma GCC dependency "file" [message]
_Pragma ("GCC dependency \"file\" [message]")

Declares that the current source file depends on file, so GNU C compares the file times and gives a warning if file is newer than the current source file.

This directive searches for file the way #include searches for a non-system header file.

If message is given, the warning message includes that text.


#pragma GCC dependency "parse.y"
_pragma ("GCC dependency \"/usr/include/time.h\" \
rerun fixincludes")
#pragma GCC poison identifiers
_Pragma ("GCC poison identifiers")

Poisons the identifiers listed in identifiers.

This is useful to make sure all mention of identifiers has been deleted from the program and that no reference to them creeps back in. If any of those identifiers appears anywhere in the source after the directive, it causes a compilation error. For example,

#pragma GCC poison printf sprintf fprintf
sprintf(some_string, "hello");

generates an error.

If a poisoned identifier appears as part of the expansion of a macro that was defined before the identifier was poisoned, it will not cause an error. Thus, system headers that define macros that use the identifier will not cause errors.

For example,

#define strrchr rindex
_Pragma ("GCC poison rindex")
strrchr(some_string, 'h');

does not cause a compilation error.

#pragma GCC system_header
_Pragma ("GCC system_header")

Specify treating the rest of the current source file as if it came from a system header file. See System Headers in Using the GNU Compiler Collection.

#pragma GCC warning message
_Pragma ("GCC warning message")

Equivalent to #warning. Its advantage is that the _Pragma form can be included in a macro definition.

#pragma GCC error message
_Pragma ("GCC error message")

Equivalent to #error. Its advantage is that the _Pragma form can be included in a macro definition.

#pragma GCC message message
_Pragma ("GCC message message")

Similar to ‘GCC warning’ and ‘GCC error’, this simply prints an informational message, and could be used to include additional warning or error text without triggering more warnings or errors. (Note that unlike ‘warning’ and ‘error’, ‘message’ does not include ‘GCC’ as part of the pragma.)

Next: , Up: Pragmas   [Contents][Index]