Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
add comment

3 Answers 3

g++ foo.c where foo.c 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 putting this in your terminal - ./a.out

g++ -o output foo.c This will compile foo.c and you can type ./output to run the compiled code.

share|improve this answer
3  
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
add comment

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
add comment

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:
$vim hello.cc
Compile hello.cc with g++:
$g++ hello.cc -o hello
Execute it:
$./hello
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. –  nitstorm Sep 14 '11 at 16:38
1  
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
    
Although if you learn vim well (and possibly emacs, I can't comment), it will make you happy, and your coding will be so enjoyable that you'll forget that you're actually doing pointless programming exercises in c++. –  belacqua Sep 14 '11 at 17:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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