C++

C++ Pragma Once

Today, we learn one of the directives of C++ language that is used to specify the compilation of the included header file once. But before jumping directly onto the main topic, let’s first take a quick look at the basics of C++ programming language. We will also learn the use of header files in the C++ program.

The C++ language, sometimes known as “C with Classes”, is a general-purpose programming language that is an extension of the C computer language. The C++ language has developed greatly over time. The current C++ contains capabilities for low-level memory management along with object-oriented, generalized, and functional features.  We use the header libraries in C++ programming to avoid having to create the code for every little thing. The complexities and number of lines of code are both reduced. Additionally, it allows you to reuse the methods that are defined in the header files for various purposes.

The C++ language directive handles #Pragma once which is used to minimize the programming errors. Although it is supported by the majority of prominent C or C++ translators including GCC and Clang translators, it is not a component of the C++ standard. If the translator is not provided, this could cause a concern about accessibility based on your program. When using #pragma once, there is an exceptional scenario where there are many header files with a similar filename.

Advantages of #Pragma Once

Let’s discuss some of the important features of the C++ preprocessor directive which is the “#pragma once”:

  • As we know, the pragma once only compiles one time in the existing program so that the implementation or code of the program is less.
  • Since we know that the compilation process of pragma is once, we have an avoidance of name dashes.
  • In other programs, when we compile the program, it takes a lot of time because the compiler compiles every file of the program. But when we use the pragma once in the program, the code of the program is less. That’s why the compilation speed of the program is fast.

Implementation

In C and C++ programming, we sometimes come across one of the preprocessor directives which is “#pragma once”. As the name suggests, “once” means it allows or makes a current source file to be included in the existing program only once at a time, only in a single compilation. Before the preprocessor directive “#pragma once”, there is another preprocessor directive that we used to overcome this problem which is “#include guards” in the C++ programming language. But pragma once only allows the compilation for one because a program that has auto program may contain several files in it.

The C++ file may include several header files. This means that the first C++ file and the second C++ file contain similar header files. The compiler compiles the first header file and then the second header file. This is because both the header files are compiled twice. We use the pragma once preprocessor directive so that the header files will compile once in the existing program.

Syntax:

Here is the writing style and implementation of the Preprocessor directive “#pragma once” in C++ programming language. The Preprocessor searches the program for specific commands that it can comprehend by calling them Preprocessor instructions. The # (hash) sign is used at the start of every preprocessor directive. First, before the translator even encounters the program, the preprocessor conducts preparatory activities like implicitly executing the code, including the files related to the program, etc.

Next, we write the “pragma” keyword which means “pragmatic information”. The pragma directive is included in the C++ standards, but the interpretation of every pragma directive is dependent upon the program that is used to execute the standard library. A technique to ask the translator for unique behavior is with the #pragma directive. This command is especially helpful for programs that need to make use of certain compiled code features or that are exceptionally large. And then, in the end, we write the behavior of the program which we want to implement on the existing C++ program which is “once”. This means that when there are many header files with a similar filename, this compiles the header file only one time.

Examples:

Let’s take an example of the preprocessor directive “#pragma once” and we will understand how this directive works in C++ language so that the mind and confusion of the user will be cleared about both the pragma directives of C++ language:

External.h

To start writing the program, we always first need a compiler where we write and execute the existing program. Install any C++ compiler. One more thing is that the pragma directive does not run on the online compiler. This example is implemented on the “Dev C++” compiler. After opening the compiler, we first create the “external.h” file where we implement the pragma directive.

external.h:
#pragma once
#ifndef EXTERNAL_H
#define EXTERNAL_H

#include

using namespace std;

 class Player
{

 public:
    int speed;
    Player()
    {
         speed = 10;
    }
    ~Player()
    {
   
    }
};
void Print()
{

cout<< "Test" <<endl;
}
bool playing = false;
#endif

First, we declare the “#pragma once” preprocessor directive that tells the compiler to only include the code in the C++ file once. This is useful if you have a header file that is included in multiple C++ files. Then, we add the #include which is a standard header that is included in all C++ programs. In the third line of the code is a #ifndef EXTERNAL_H. This is a compiler directive that is used to create an external header file. Then, we implement the #define EXTERNAL_H again.

After that, we declare the public class name “Player” which tells the compiler that the Player class is public. Then, we declare the integer type variable “speed” and initialize the value to it. In the next line, we create a Player() constructor. Now, we create the function of print() and pass the cout() method in it. We declare a Bool-type variable playing and initialize the false value to it. This is a member variable declaration for the playing member variable. In the end, we use the #endif because it is a compiler directive that is used to end the #ifndef statement.

Main.Cpp File

First, we include the file which we already created – external.h file. Then, we include the “#include <iostream>” that is used to get the data from the user and display the data to the user. Then, we create the player object of type player which we already defined in the external.h file. Then, we call the player and concatenate it with the speed variable that we initialized in the external file and print it using the cout() method. We use the if-statement so that we check if the player is playing. If it is true, it displays the player. If it is false, it displays nothing. In the end, we return 0 to the main() function so that we can stop the execution of the program.

main.cpp:
#include "external.h"
#include

using namespace std;
 int main ()
 {

     Player player;
cout<< "The speed of the player is: " <<player.speed<<endl;
     if(playing)
     {
        Print();
     }
     return 0;
}

Conclusion

In this article, we learned about one of the important preprocessors of C++ which is “#pragma once”. We learned why we use pragma once and we also discussed its benefits. We implemented the example of pragma once with a detailed explanation of pragma once directive so that no point of confusion is left for the user.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.