Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to Linux. I am using Ubuntu 11.04 and do not know how to compile and execute C++ program in it. I need to know the commands to Compile and Execute a C++ program in Linux.

share|improve this question

To compile your c++ code, use:

g++ foo.c

foo.c in the example is the name of the program to be compiled.

This will produce an executable in the same directory called a.out which you can run by typing this in your terminal:


g++ should already be in your $PATH, so you don't need to call /usr/bin/g++ explicitly, but you can use the latter in any case.

foo.c should be in the same directory you're running the command from. If there is any doubt, you can make sure you are in the same directory by typing ls foo.c or head foo.c (if you need to verify you're working with the correct foo.)

As noted by @con-f-use, the compiler will usually make this file executable, but if not, you can do this yourself (so the command to execute, ./a.out or equivalent, will work):

chmod +x ./a.out

To specify the name of the compiled output file, so that it is not named a.out, use-o` with your g++ command.

g++ -o output foo.c

This will compile foo.c to the binary file named output, and you can type ./output to run the compiled code.

share|improve this answer
The compiler usually makes the binary (a.out in this case) executable. If not you can do so by typing: chmod +x a.out. When your compiled program is executable, you can run it typing ./a.out - the dot and the slash indication, that you want to execute it. – con-f-use Sep 14 '11 at 15:57
@Rajeshkumar, did you find one of these answers to your liking? If so then could you please mark one of them as the accepted answer (by selcting the tick beneath the up/down vote arrows) so we can draw a line beneath this issue. – Chris Wilson Oct 11 '11 at 12:47

I'm making two assumptions here:

  1. You already have a C++ source file/program ready to build
  2. You have set up a build system on your computer

The simplest way to compile a C++ program on Ubuntu, or any other Linux distro for that matter, is to type

g++ main.cpp -o main
  • g++ is the invocation of the C++ component of GCC, the defacto compiler for C/C++ and whole host of other languages on the Linux platform. It's currently the only compiler capable of compiling the Linux kernel.
  • main.cpp is the c++ source file you wish to compile.
  • -o main specifies the name of the output file you wish to create once the source is compiled. The target source file and the target output file can be inverted if you wish, so g++ -o main main.cpp is equally valid.
  • To then execute that program, you need to do ./main in the terminal.

The above commands assume you are already in the location of the source files, but both the source file and target output file may also be specified as a directory. For example

g++ ~/Desktop/main.cpp -o ~/Projects/main

will compile a C++ source file located on your desktop and place the executable binary in a Projects folder in your home directory. To run this executable, run ./Projects/main.

share|improve this answer

You need g++,for gcc may can't compile cpp file easily.
You also need to learn vim or emacs,to write C code.
Just try this on your terminal:

Type a test program and save it:
Compile with g++:
$g++ -o hello
Execute it:
Here the "./" means the exe file is under the current dir.

share|improve this answer
gcc is the GNU compiler for C and C++ compiler. And the OP does not necessarily need to know vim or emacs to write C code, there are lot of other text editors and IDE's floating around. – Nitin Venkatesh Sep 14 '11 at 16:38
although I love vim, if somebody already struggles with finding out how to run a problem, suggesting vim is not very useful. – johanvdw Sep 14 '11 at 16:45
g++ filename.cpp 


g++ One.cpp

This will generate output file named as (e.g.):


If you create another program and compile that (e.g.):

g++ Two.cpp

This will also generate output file named as (e.g):


This means you can get the output of only last compiled program if you execute that. To avoid this use (e.g.)

g++ One.cpp -o One 

now, your output file will be :


Now, you can execute the file with the name specified by you.

share|improve this answer

protected by A.B. Jul 11 '15 at 8:52

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.