128

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.

4

5 Answers 5

158

To compile your c++ code, use:

g++ foo.cpp

foo.cpp 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:

./a.out

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.cpp 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.cpp or head foo.cpp (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.

E.g.

g++ -o output foo.cpp

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

4
  • 7
    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, 2011 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.
    – user2405
    Oct 11, 2011 at 12:47
  • Is there a difference between g++ foo.c -o output and g++ -o output foo.c? Oct 1, 2016 at 2:51
  • Why does this not work with gcc?
    – J-Cake
    Jun 17, 2020 at 3:25
25

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.

6

This is how I like to compile with g++.

$g++ -W -Wall -pedantic -o programName -p sourceFile.cpp

-W: Print extra warning messages for some problems.
-Wall: Enable all the warnings about questionable code
-pedantic: Show all the warnings demanded by strict ISO compliance
-o programName: place the executable output in programName sourceFile.cpp: the 
name of our source code file
-p: Generate extra code to write profile information suitable for the analysis program prof
2
  • 1
    -p: Generate extra code to write profile information suitable for the analysis program prof.
    – Hans
    Dec 16, 2016 at 8:08
  • I like your answer. It brings extra suggestions. But you should really re-phrase it to be generally acceptable. Nov 6, 2017 at 6:39
1

g++ is a front-end to gcc(GNU Compiler Collection) with some predefined c++ macros and different default options/flags.

compiling c++ code with gcc is handy when g++ is not available for any number of reasons ,in fact it's just a matter of linking to the c++ library -lstdc++ and informing gcc to treat the input as c++ code (either by using a .C extension , or forcing the language with -x)
other valid c++ file name suffixes : .cc , .cp , .cxx , .cpp , .CPP , .c++

example :

gcc cpp_code.C -lstdc++ the uppercase extension (.C) is important for gcc to know it's a c++ file.

or explicitly specifying the input language :

gcc -x c++ cpp_code.txt -lstdc++ extension can be anything, or even nothing

by default the result (after a successful compilation) is an a.out file which can be run with ./a.out

1
1

You need g++, for gcc may not 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.

3
  • 3
    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. Sep 14, 2011 at 16:38
  • 4
    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, 2011 at 16:45
  • 1
    I personally do not like vi, but it does not matter. You should stray away from suggesting a specific editor, everyone uses his/her own anyway. Nov 6, 2017 at 6:36

You must log in to answer this question.