How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

August 24, 2018
How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

“How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS” will demonstrate how to install a C compiler on Ubuntu 18.04 LTS, create a demo C++ program, build an executable from the demo program, and run the program in a terminal. We find that there is no need for a bloated IDE like Microsoft Visual Studio to create simple, terminal-based programs, and that creating and running C and C++ programs on Linux is much more straight-forward and faster than configuring and using an IDE.

1. The first thing to do once your Ubuntu machine is started and you are logged in is to update your system. Open a terminal and enter the following command to update your Ubuntu 18.04 LTS system:

sudo apt-get update
How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

2. Once the update completes, we can install the build-essential package. The build-essential package includes everything needed to compile and run C & C++ programs on Linux. The gcc/g++ compilers are part of the build-essential package. We will also install the manpages-dev package, which will help out in a bind. In your terminal, enter the following command to install the needed packages:

sudo apt-get install build-essential manpages-dev -y
How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

3. We can verify the installation of the packages by entering the following command. The command will display the path to the gcc executable and the version of gcc that was installed.

which gcc && gcc –version
How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

4. The output of the previous command should look similar to the image below:

How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

5. We will now create a simple program in C++ via the terminal-based text editor nano. To create the program, simply enter the following command into the terminal:

nano HelloWorld.cpp

NOTE: To create a C program, simply change the extension from .cpp to .c

How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

6. When the text editor opens, copy the following code and paste it into the text editor:

#include <iostream>
using namespace std;

int main()
{
    printf("Hello, World!\n");
    return 0;
}
How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

7. Save the program by entering CTRL + o, then close the text editor by entering CTRL + x. Now we can build an executable from the program we just created. To build the executable, enter the following into the terminal:

make HelloWorld
How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

8. The output of the previous command should look like the image below:

How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

9. Now that our C++ program is built, we can execute it by entering the following command into the terminal:

./HelloWorld
How to Create, Compile, and Run C & C++ Programs on Ubuntu 18.04 LTS

10. When the program executes, you will see the phrase “Hello World!” displayed in the terminal. That means the program works! Congratulations! That’s all there is to installing and using a C and C++ compiler on Ubuntu 18.04 LTS. Find the code to accompany the tutorial on GitHub!