1.Open up a terminal on Ubuntu Linux and install the build-essential package by typing the following command in the terminal
sudo apt-get install build-essential
2.Create a directory and a sub directory to hold your C/C++ programs and your main HelloWorld program.
mkdir -p CCPP/HelloWorld
3.Then we will change into our created directory by issuing the following command
cd CCPP/HelloWorld
4.Next we will use a text editor such as gedit or nano to create our C or C++ source code using the following command.
5.For example for a C source code file we would issue the following command
gedit main.c
6.Enter the following C source code below:
- #include<stdio.h>
- #include<stdlib.h>
- int main()
- {
- printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");
- return(0);
- }
- 7.Save the file as main.c and exit
- 8.For example for a C++ source code file we issue the following command
- gedit main.cpp
- 9.Add the following lines below to create your C++ source code:
- #include<iostream>
- using namespace std;
- int main()
- {
- cout<<"\nHello World,\nWelcome to my first C ++ program on Ubuntu Linux\n\n"<<endl;
- return(0);
- }
- 10.Save the file as main.cpp and exit
- 11.Compiling your C/C++ program
- Important: 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
- Type/Copy/Paste: 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 called HelloWorldC.
- The options -Wall -W and -Werror instruct the compiler to check for warnings.
- Type/Copy/Paste: gcc -Wall -W -Werror main.c -o HelloWorldC
-
13If you are compiling the C++ program version of Hello World type in the terminal
- Type/Copy/Paste: g++ -Wall -W -Werror main.cpp -o HelloWorldCPP
- Type/Copy/Paste: g++ -Wall -W -Werror main.cpp -o HelloWorldCPP
-
14If 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/Copy/Paste: chmod +x HelloWorldC
- or
- Type/Copy/Paste: chmod +x HelloWorldCPP
- Type/Copy/Paste: chmod +x HelloWorldC
-
15In order to execute your program you will have to type in the following commands.
- To execute the C program version of the program:
- Type/Copy/Paste: ./HelloWorldC
- To execute the the C++ program version of the program:
- Type/Copy/Paste: ./HelloWorldCPP
- To execute the C program version of the program:
- 12.If you are compiling the C program version of Hello World type in the terminal