- How to Run C/C++ Programs in Linux [Terminal & Eclipse]
- Prerequisite: Install build-essential
- Method 1: Compile and run C++ program in Linux terminal
- Compile C++ code in the Linux terminal
- Run C++ code in the Linux terminal
- Method 2: Setup Eclipse for C++ programming in Ubuntu Linux
- How can I compile and run C/C++ code in a Unix console or Mac terminal?
- How to Compile and Run a C++ Program in a Linux Terminal
- How to Compile and Run a C++ Program in the Linux Terminal
- Conclusion
- About the author
- Komal Batool Batool
How to Run C/C++ Programs in Linux [Terminal & Eclipse]
I have been requested more than once to write an easy-to-follow tutorial to run C++ programs in Linux. In this guide, I’ll discuss:
The process is pretty much similar to running C program in Linux.
Do note that I am using Ubuntu Linux while writing this article but the same steps are valid for other Linux distributions based on Ubuntu, such as Linux Mint, elementary OS, etc.
Prerequisite: Install build-essential
If you want to do coding in Ubuntu Linux, you should install build-essential package. It consists of various software that you will need to compile programs, including gcc and g++ compilers.
You may install gcc on Ubuntu and other distributions separately as well but the build-essential has additional tools that you may need.
Normally, build-essential should already be installed on your system. But to make sure, run the command below:
sudo apt install build-essential
Method 1: Compile and run C++ program in Linux terminal
Once the build-essential is installed, you are ready to code in C++. I believe that you already know how to code in C++, even a little bit. Our main aim is to see how to compile and run C++ programs in the terminal.
Let’s take an example of the swap program which I wrote in a file named swap.cpp. The content of this file is the following:
You can save the program wherever you want.
Compile C++ code in the Linux terminal
To compile the program, go to the directory where you have saved the cpp file and use the command in the following format:
Basically, with the -o option, you are telling the compiler to generate the executable code in file swap. If you don’t do that, it will default to a.out file, which is not a good programming practice.
Run C++ code in the Linux terminal
Once you have compiled the code, you’ll get the executable file. You just need to run it in the following manner:
You can refer to this gif for a better demonstration of running a C++ program in Ubuntu Linux.
Method 2: Setup Eclipse for C++ programming in Ubuntu Linux
That was the basic way of running a C++ program in Linux. But if you are working on a C++ project, building and running individual files would be a nightmare.
This is where Integrated Development Environment (IDE) comes in picture. One can argue a lot about the best IDE for Linux, but if you ask for my advice, I’ll say go ahead with Eclipse. This is the best IDE for C++ development, in my opinion. Did I mention that it is also open source?
Recommended Read:
How can I compile and run C/C++ code in a Unix console or Mac terminal?
where the source file is foo.c, foo.cpp, etc., you don’t even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus the extension.
Running the executable just built is the same as running any program — but you will most often need to specify the path to the executable as the shell will only search what is in $PATH to find executables, and most often that does not include the current directory ( . ).
So to run the built executable foo :
I didn’t realize the builtin rules propagated to targets specified when invoking make. Learned something new today =)
«-bash: make: command not found»
@FabianAmran It refers to the current directory. The shell will look only in the directories listed in the $PATH environment variable for programs to execute unless a path is specified when running the program. . (current directory) is often not in $PATH for security reasons.
gcc main.cpp -o main.out ./main.out
I used «gcc main.cpp -o main.out», and get this error, Undefined symbols for architecture x86_64: «std::__1::locale::use_facet(std::__1::locale::id&) const», . turns out the reason is, gcc default-links is libc. while using g++ will link with libstdc++. So use «g++ main.cpp -o main.out» may be better.
About Undefined symbols for architecture x86_64 issue, I modify the command as follows: gcc -lstdc++ main.cpp -o main.out , and that works on my Mac. via link:stackoverflow.com/questions/11852568/…
This is the command that works on all Unix machines. I use it on Linux/Ubuntu, but it works in OS X as well. Type the following command in Terminal.app.
-o is the letter O, not zero
lab21 will be your executable file
iterative.cpp is your C++ file
After you run that command, type the following in the terminal to run your program:
All application execution in a Unix (Linux, Mac OS X, AIX, etc.) environment depends on the executable search path.
You can display this path in the terminal with this command:
On Mac OS X (by default) this will display the following colon separated search path:
So any executable in the listed directories can by run just by typing in their name. For example:
This runs /bin/cat and displays mytextfile.txt to the terminal.
To run any other command that is not in the executable search path requires that you qualify the path to the executable. So say I had an executable called MyProgram in my home directory on Mac OS X I can fully qualify it like so:
If you are in a location that is near the program you wished to execute you can qualify the name with a partial path. For example, if MyProgram was in the directory /Users/oliver/MyProject I and I was in my home directory I can qualify the executable name like this, and have it execute:
Or say I was in the directory /Users/oliver/MyProject2 and I wanted to execute /Users/oliver/MyProject/MyProgram I can use a relative path like this, to execute it:
Similarly if I am in the same directory as MyProgram I need to use a «current directory» relative path. The current directory you are in is the period character followed by a slash. For example:
To determine which directory you are currently in use the pwd command.
If you are commonly putting programs in a place on your hard disk that you wish to run without having to qualify their names. For example, if you have a «bin» directory in your home directory for regularly used shell scripts of other programs it may be wise to alter your executable search path.
This can be does easily by either creating or editing the existing .bash_profile file in your home directory and adding the lines:
Here the tilde (~) character is being used as a shortcut for /Users/oliver. Also note that the hash bang (#!) line needs to be the first line of the file (if it doesn’t already exist). Note also that this technique requires that your login shell be bash (the default on Mac OS X and most Linux distributions). Also note that if you want your programs installed in ~/bin to be used in preference to system executables your should reorder the export statement as follows:
How to Compile and Run a C++ Program in a Linux Terminal
For many developers, Linux is an ideal environment to write C++ code because of its open-source nature and powerful compiler. However, compiling and running C++ programs on Linux could be intimidating for users who are not familiar with the procedure. Fortunately, with the right instructions and tools, it can be made simple. This tutorial aims to guide you through the process of compiling and running C++ programs on Linux, breaking it down into easy-to-follow stages.
How to Compile and Run a C++ Program in the Linux Terminal
Compiling C++ programs on Linux is a straightforward process that can be achieved using the GNU Compiler, also known as g++. This command-line program converts your high-level language code into an executable file. If you have some knowledge of C++ programming, our main focus is to teach you how to compile and run C++ programs in the terminal.
To compile a C++ program in a Linux terminal using a g++ compiler, follow the below-given steps:
Step 1: First create a cpp file using the nano editor and paste your C++ code in it:
Here I am using the following code as an example.
Save the file using “Ctrl+X”, add “Y” and enter to exit.
Step 2: To compile the program, navigate to the directory in which you saved the .cpp file and run the following command:
Essentially, the -o option specifies the name of the output file that the compiler will create.
Note: If your program includes mathematical functions:
Step 3: Now run the output file using the following command:
This will run our code and gives you the output.
Conclusion
Compiling and running a C++ program in a Linux terminal is a simple procedure that may be achieved by utilizing a few easy commands. Users can quickly compile their code using the g++ command and run their program by executing the resulting binary file. However, they must first create a cpp file, add the C++ code to it and then run the file according to the above-given steps.
About the author
Komal Batool Batool
I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.