C++ pragma directive

Pragma is a compiler directive that allows us to add additional information to the compiler so that it can compile the code effectively. This information, once added, is not then in our hands to control it and can change the details of compilation. This directive is used to access compiler-related specific processor extensions.

Pragma in C++

The pragma in this programming language is a directive and is provided by the standard of C++ to give extra details required to the compiler. These details are unable to pass by arguments in the program or through the logic of a code. These derivatives are responsible for carrying a special purpose and turning on or off the feature inside the program.

Working of pragma

Now we will understand the working of pragma directives. We already know that these pragmatic directives are used to notify the compiler about an event or the programs that will happen. These pragma directives execute before the compilation and then are followed by the processing of the programs. By doing this, a pragma can notify the compiler to neglect some things and at the same time prefer those that are to be compiled in the future. Most programmers recommend adding pragma in a program either only once. These sorts of directives are very compiler-specific because they vary from compiler to compiler. Not all compilers use the same pragma. This means that Pragma directives are machine and operating system dependent too.

Syntax of Pragma directive

There exist two types of syntax for pragma, in which it is implemented in a program. The parameters and keywords are the same in both the syntaxes, but the difference is initiating the code statement. The syntax that is known as the standard for implementation of pragma begins with the hashtag and the pragma keyword with a simple string known to be a ‘token string.

# pragma token-string

Whereas the second syntax resembles the first syntax, but the difference is that the operator before the pragma keyword is “__”.

# __pragma(token-string)

This syntax uses two underscores before the keyword. Some of the most commonly used token strings are exit, warn, etc. We have explained them briefly here:

# pragma startup

Before we use the main program’s code to be executed, the specified function present in pragma is required to execute first because this causes the compiler to run the program by acting as an initiative for the execution.

# pragma exit

Like the startup, this function specified in the pragma is needed to run as earlier before the program is ended.

# pragma warn

This is a specific feature. These functions hide the error warnings or those annoying warnings that are shown very often after every code execution. But sometimes, some important warnings are also ignored because of this feature.

# GCC dependency

It checks the dates of the file that is currently used and the other files too. If the other file is the most recent, then it displays the warning for that file.

# pragma GCC poison

This is used to block the identifier from the source code or a program.

Now we will use each of them in examples to explain the working of Plasma directives.

Plasma startups and pragma exit

These directives are used to specify the functions. Identifying the specific ones to run first or later. These directives, as we have mentioned before, needed to run before the starting of the program.

And similarly, in the case of closing, just before the program exists, these directives are executed first. Here we will add the pragma directive in the code. The below image contains a glimpse of the whole code, in which we have used the directives directly, but unfortunately, this program doesn’t run with the GCC and G++ compilers. So we have updated the program, source code to execute the desired output. Two functions with the main program are declared here.

The output shows that the function and the main programs are executed, and the answer is displayed by using a g++ compiler.

$ g++ -o p p.c
$ ./p

Pragma warn directive

This is used to hide the messages that contain the warning that is displayed during the compilation. These warning issues are useful in the case when some urgent and large error occurs. This feature is used in the case of large source codes, and so we can solve all the errors by hiding all the warnings that are shown. If we change the syntax slightly, these warnings do not remain hidden and are visible again.

# pragma warn +xxx (To show the warning)
# pragma warn -xxx (To hide the warning)

These two syntaxes are used to hide and unhide the warnings. Those warnings that can be hidden are listed below:

  • #Pragma warn –rvl: This type of directive hides the warnings that are raised when we design a function to return the value but act as a ‘void’.
  • #pragma warn –par: Those warnings are hidden by this pragma directive that is raised when a function doesn’t use the parameters that are passed to it.
  • #pragma warn –rch: Those warnings are affected by this directive that is raised; we cannot reach the code. For instance, if a code is written after using the return statement, it is not reachable.

Now we will take an example. In this example, we have initialized all three pragmas warn directives.

In the main program, we use a return type ‘int’ and a variable ‘x’ in the parameter. A simple word is displayed simply no return statement is used. Similarly, the variable in the argument is not used inside the function or main program.

So there are chances of two warnings to take place. Both will hide because of two directives. One is pragma warn –rvl, and the second will be pragma warn –par. But both of these warnings will take place without appearing. And hence the program will compile successfully, and the answer will be displayed.

#Pragma GCC poison

This is supported by the GCC compiler and helps remove the identifier from the source code completely from the program. Now we will apply this directive as an example.

By using this directive, we have also initialized a variable here to use in the condition and print two words if the condition is successful.

But when you compile the code and execute it, the following error will occur the line number where the error has occurred.

#Pragma GCC dependency

It allows us the date comparison of the current and the second file. If the date of the other file is the latest, then a warning notification is displayed. This will be favorable when the current file is regenerated from the second file.

# pragma GCC dependency "/usr/folder/time.h" rerun fixfolder


The article ‘C++ pragma derivatives” aims to facilitate the user with the basic description of Pragma and its working in the programming languages, especially in C++. Two syntaxes are explained with all possible string tokens, and each derivative example is well explained to acknowledge the beginners with the easily understandable concept with the implementation of each pragma in the Linux environment operating system.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.