How to Compile a C/C++ Program on Ubuntu Linux

If you are running a Linux operating system, read this article on how to compile and run a C++ program. Even though you do not yet understand the C++ language, you can follow the operational steps to compile a program from C++ to machine code.

This article will show you how to compile a C/C++ program on Ubuntu Linux using the GNU gcc/g++ compiler. Additions were made in order to simplify and clarify the creation of a C/C++ program on Ubuntu Linux. Hopefully this article will serve as a guide and tutorial to compiling GNU C/C++ programs on Ubuntu Linux.


1. Open up a terminal on Ubuntu Linux and install the build-essential package by typing the following command in the terminal.

  • Type: sudo apt-get install build-essential 
  • This will install the necessary C/C++ development libraries for your Ubuntu Linux system to create C/C++ programs.


2. Create a directory and a sub directory to hold your C/C++ programs and your main HelloWorld program.

  • Type: mkdir -p CCPP/HelloWorld
  • We are using CCPP for the main directory to hold our created C/C++ programs which stands for C and C+ + programs directory and we are using the sub directory HelloWorld to hold our main program.


3. Then, change into the created directory by issuing the following command.

  • Type: cd CCPP/HelloWorld


4. Next, use a text editor such as gedit or nano to create C or C++ source code using the following command.


5. For example, for a C source code file, use the following command.

  • Type: gedit main.c
  • or
  • Type: nano main.c


6. Enter the following C source code below.

  • Type:
  int main() 
  printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");


7. Save the file as main.c and exit.


8. For example, for a C++ source code file, use the following command.

  • Type: gedit main.cpp
  • or
  • Type: nano main.cpp


9. Add the following lines below to create your C++ source code.

  • Type:
using namespace std;
int main()
    cout<<"\nHello World,\nWelcome to my first C ++ program on Ubuntu Linux\n\n"<<endl;


10. Save the file as main.cpp and exit.


11. When compiling your C/C++ program, make sure you are in the CCPP/HelloWorld directory before you compile your C/C++ programs.


12. If you are compiling the C program version of Hello World type in the terminal, use the below code. 

  • Type:  gcc -Wall -W -Werror main.c -o HelloWorldC
  • The first line will invoke the GNU C compiler to compile the file main.c and output (-o) it to an executable file called HelloWorldC.
  • The options -Wall -W and -Werror instruct the compiler to check for warnings.


13. If you are compiling the C++ program version of Hello World type in the terminal, use the below code.

  • Type: g++ -Wall -W -Werror main.cpp -o HelloWorldCPP


14. If you should happen to get permission errors, you need to make the file executable. You can do this by issuing the following commands below.

  • Type: chmod +x HelloWorldC
  • or
  • Type: chmod +x HelloWorldCPP


15. In order to execute your program you will have to type in the following commands.

To execute the C program version of the program:

  • Type: ./HelloWorldC

To execute the the C++ program version of the program:

  • Type: ./HelloWorldCPP



  • If you don't use the -o option the name of the executable will be a.out (by default).

Source: Wikihow,
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.

Last modified: Wednesday, July 14, 2021, 1:36 PM