The C programming language was created mainly as a system programming language that can be used to write a kernel or an operating system. This article gives you a complete list of ways to solve How to Compile a C++ Program in Linux, as well as step-by-step instructions.
Because of how well it works, the C++ Programming Language is used to make games, desktop apps, operating systems, browsers, and so on. In this article, we will use different compilers, such as the CC, GCC, and G++ compilers, to compile and run C Programming Language and C++ code.
How to Compile a C++ Program in Linux
- Open a terminal: You can open a terminal by pressing Ctrl+Alt+T or by searching for “Terminal” in the applications menu.
- Navigate to the directory where your C++ program is located: Use the
cd
command to change the directory. For example, if your program is located in the “Documents” folder, you can typecd Documents
to navigate to that folder. - Use a text editor to write your C++ program: You can use a text editor like nano, vim, or gedit to create and edit your C++ program. For example, you can type
nano program.cpp
to create a new file named “program.cpp” and open it in the nano text editor. - Write your C++ program: Use the text editor to write your C++ program code. Make sure to save the file when you’re done editing.
- Compile the C++ program: In the terminal, use the
g++
compiler to compile your program. The basic syntax isg++ program.cpp -o program
, where “program.cpp” is the name of your source code file and “program” is the desired name for the compiled executable. For example, you can typeg++ program.cpp -o program
to compile your program and generate an executable named “program”. - Run the compiled program: After successful compilation, you can run the program by typing
./program
in the terminal. Replace “program” with the name you provided in the compilation step.
Prerequisites
- A text editor: This can be any text editor of your choice, such as Vim, Emacs, or Nano.
- A C++ compiler: We will be using the GNU Compiler Collection (GCC) in this article, which should already be installed on most Linux systems.
- The GNU Make build tool: This is a tool for automating the build process of your code. It may already be installed on your system, but if not, you can install it using your package manager.
FAQs
The keyboard shortcut is Ctrl + Alt + T. You can also click the Terminal icon in your Apps menu. It generally has an icon that resembles a black screen with a white text cursor. Type the name of the program and press ↵ Enter .
In languages such as C/C++ and GO, we parse the source code and output direct machine code in a compiled file, easy enough; This is also not true. There is nothing in the language specification of C, C++, or Go that says that it has to be implemented by a compiler that produces machine code.
Programs can be created and modified using the Update Processor. Additionally, programs can be compiled when exiting the Update Processor by using either the CTRL+XC command, which compiles and catalogs the program, or the CTRL+XR command, which compiles and runs the program. Any other editor can also be used.