Tell me more ×
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

4 Answers

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
2  
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:
$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

First of all: to compile and link c++ programs you will need to install a compiler and different development libraries and other programs. The best way this in ubuntu is by installing the build-essential package.

From you question it is unclear whether you want to compile an existing or a new program. If you want to compile a new program yourself you can get started with using g++ as outlined by other people. But quickly you will discover that this is insufficient and you will split the project into compiling and linking your program. To avoid repetitive actions this is usually done by creating a Makefile, which can be run through make. If you intend to compile a program created by someone else, this is usually how you get started.

I know I got a lot of downvotes for this statement, but I think that if you really want to develop a program in C++ you should get a book. There are just too many steps involved, and since many goods books exist it would be a waste of time to reproduce all of that here. If I would, I would end up with something similar to this site: http://konst.org.ua/articles/en/linuxcom.gnutools.html (which handles C). Perhaps that can get you started.

share|improve this answer
The asker is not asking for this. The asker is asking for compiler usage instructions. And people can give full answers here. – nickguletskii Sep 14 '11 at 16:56
any reasonable program has more than one file, and is linked to at least one external library. – johanvdw Sep 14 '11 at 17:01
1  
And? Wildcards and multi-arguments exist for that reason. Plus, what is stopping you from writing a detailed explanation, including makefiles? This is not acceptable for an answer. – nickguletskii Sep 14 '11 at 17:05
I suggest you to remove the answer and post a new one next time if you completely change your answer instead of making small improvements. – Lekensteyn Sep 15 '11 at 9:26
My core answer: get a book is still there, and I'm still convinced that this is the only good answer. The other answers prove that. I don't know any real c++ program which i could compile with their instructions. – johanvdw Sep 15 '11 at 9:32

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.