What is a command to compile and run C++ programs?
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.
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.
I'm making two assumptions here:
- You already have a C++ source file/program ready to build
- 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
.
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
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
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.