C++

C++ __FILE__ macro

The macros are some piece of code in some script having some specific name. When any of the macros got executed, the code behind them is executed to perform a certain task. Just like that, the __FILE__ is a macro used within the C++ language to get the path to a specific file. So, today in this guide, we will be discussing the working of a C++ __FILE__ macro.

Example 01:

Let’s take the first example of __FILE__ macro in C++ by starting the terminal console application. You might be using the shortcut “Ctrl+Alt+T” to do so. Another way to open the shell is to use the menu Activity area if you forgot the shortcut key. After the terminal console is launched on your screen, try creating a new c++ file to do code in it. A file can be created with a simple “touch” query, as shown in the attached image below. The name of a newly created file must be given with the “touch” keyword.

There must be some editor installed on your Ubuntu 20.04 Linux system to make use of it for coding and opening the newly created files. The most recommended editors widely known among Linux users are GNU Nano and Vim editor. Other than that, the text editor can also be utilized in case both the previously mentioned editors are not working. So, we have already installed the GNU Nano editor. Thus, we will be using it to open and edit the file to make code. Use the “nano” keyword along with the file name to make the editor work.

Now, the newly created C++ file macro.cc is ready for coding in the editor. Let’s start implementing C++ code as we are implementing this article to see the working of __FILE__ macro. We have started our C++ code by adding the standard input-output header at the top of it. The keyword “include” has been used with the hash sign to do so. Without this header, the input and output of the C++ code will not be going to work at the console upon compilation and execution.

The main() method is the basic pillar of our code as the execution starts from it and ends in it. Within the main() method, the actual work will be done. We have been using a single line code in our main() method to describe the functionality of __FILE__ macro. So, the printf statement has been used here, taking the two values as arguments, i.e., string taking the file name and macro function.

The __FILE__ macro is used here to acquire the current file path or its name. While the “%S” is used to print out the current path of a file or its name within a string. You must save your code before the execution with the help of a “Ctrl+S” shortcut. Now the code is saved, try getting back to the shell terminal using the “Ctrl+X” shortcut.

As we are done with the C++ code for __FILE__ macro, let’s begin the compilation of C++ code. For the compilation, we have already installed the g++ compiler to compile the C++ code. So, the g++ compiler has been utilized in the terminal to compile the file “macro.cc”. Upon compilation, it returns nothing, which indicates that the C++ code is inside the macro.cc file is semantically correct. After the compilation, the turns come for the execution of a code. To run the code file, we tend to use the “./.aout” simple query. Upon running the file macro.cc, we have got the name of a file as in return. This is how the C++ __FILE__ macro works.

Example 02:

Let’s have another example to go deep down to look upon the functionality of C++ __FILE__ macro within the Ubuntu 20.04 system. As we have seen how the C++ __FILE__ macro works when it has been used to fetch the file path of a file in which it has been mentioned.

Now, we will use the C++ __FILE__ macro to get the file path or name of a file other than the file we are currently working on. So, we have opened the same file macro.cc in the GNU Nano editor to update the code. We have added the standard input-output header with include keyword and initialized a main() function with the integer return type.

Within the main() method, the first print statement is the same as the old one to get the current file path on which we are currently working. After that, we have used the #line keyword with another file name, i.e., “new.txt” at the next consecutive line. This is to specify the file other than the current file for which we will be getting its name or a path. The next printf statement has been utilized to print out the path of a file “new.txt” with the help of a C++ __FILE__ macro. Save your newly updated code to avoid any inconvenience at the compilation time. Use “Ctrl+S” to do so.

After saving the updated file, quit it via the “Ctrl+X” from your keyboard. Now, we are back on the shell screen.

Let’s compile the updated file. Use g++ compiler to do so with the name of a file macro.cc. After the successful compilation, run the code with the “./a.out” command in the console. The output shows the path to a current file first, then the path to the other file, “new.txt” has been displayed on the shell screen. Look how easy it is to use the __FILE__ macro to display the file path on the shell.

Example 03:

Let’s have a look at the __LINE__ macro along with the __FILE__ macro in C++. The __LINE__ macro will display the line number of the file on which the macro has been used. So, open the same file and update both the printf statements. Before the C++ __LINE__ macro, we have added the C++ __LINE__ macro. This __LINE__macro is getting the line number of a current file where the __FILE__ comes first. While the next __LINE__ macro will get the first line number of the other file because we have already mentioned the line number with the file name in the code.

After saving and quitting the file, we have returned to the shell terminal. We are utilizing the same g++ command to compile the C++ macro.cc file code. The successful compilation leads to the execution command. Now the “./a.out” command is used here to do so. The execution tells us that the first file, i.e., the current file in line 3, while the __FILE__ macro is on the first line of the other file mentioned in the code.

Conclusion:

This article contains the definition of macros and discusses the usage of C++ __FILE__ macro in the Ubuntu 20.04 system. The C++ __LINE__ macro has also been discussed within some examples to illustrate the working of C++ __FILE__ macro more. To sum up, we have a high hope that this article will help you with the C++ __FILE__macro at its best.

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.