3/28/2014 How to Compile a C/C++ Program on Ubuntu Linux: 15 Steps How to Compile a C/C++ Program on Ubuntu Linux 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/Copy/Paste: 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/Copy/Paste: 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 we will change into our created directory by issuing the following command Type/Copy/Paste: cd CCPP/HelloWorld 4 Next we will use a text editor such as gedit or nano to create our C or C++ 5 For example for a C source code file we would issue the following command source code using the following command. Type/Copy/Paste: gedit main.c or Type/Copy/Paste: nano main.c http://www.wikihow.com/index.php?title=Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux&printable=yes 1/4 3/28/2014 How to Compile a C/C++ Program on Ubuntu Linux: 15 Steps 6 Enter the following C source code below: Type/Copy/Paste: #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 Type/Copy/Paste: gedit main.cpp or Type/Copy/Paste: nano main.cpp 9 Add the following lines below to create your C++ source code: Type/Copy/Paste: #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. http://www.wikihow.com/index.php?title=Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux&printable=yes 2/4 3/28/2014 How to Compile a C/C++ Program on Ubuntu Linux: 15 Steps 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 Type/Copy/Paste: 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/Copy/Paste: chmod +x HelloWorldC or Type/Copy/Paste: 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/Copy/Paste: ./HelloWorldC To execute the the C++ program version of the program: Type/Copy/Paste: ./HelloWorldCPP Add your own method Name your method Add your steps using an ordered list. For example: 1. Step one 2. Step two 3. Step three Save Warnings If you don't use the -o option the name of the executable will be a.out (by default). Article Info http://www.wikihow.com/index.php?title=Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux&printable=yes 3/4 3/28/2014 How to Compile a C/C++ Program on Ubuntu Linux: 15 Steps Categories: Ubuntu Recent edits by: KamiDesu, Entricular, BR Thanks to all authors for creating a page that has been read 77,174 times. http://www.wikihow.com/index.php?title=Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux&printable=yes 4/4