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.
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.
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.
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.
6. Enter the following C source code below.
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.
9. Add the following lines below to create your C++ source code.
using namespace std;
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.
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
-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.
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.
chmod +x HelloWorldC
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:
To execute the the C++ program version of the program:
- 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
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.