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.

Steps


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.

Compile-1

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.

Compile-1

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

  • Type: cd CCPP/HelloWorld

Compile-3

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

Compile-4

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

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

Compile-5

6. Enter the following C source code below.

  • Type:
#include<stdio.h>
#include<stdlib.h>
  int main() 
  {
  printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");
  return(0);
  }
 

Compile-6

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

Compile-7

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

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

Compile-8

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

  • Type:
#include<iostream>
using namespace std;
int main()
 {
    cout<<"\nHello World,\nWelcome to my first C ++ program on Ubuntu Linux\n\n"<<endl;
    return(0);
 }

Compile-9

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

Compile-10

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

Compile-11

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.

Compile-12

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

Compile-13

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

Compile-14

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

Compile-15

Warnings

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

Source: Wikihow, http://www.wikihow.com/Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux
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