http://cpp.gantep.edu.tr
C++ resources. Faculty of Engineering, University of Gaziantep
 MAIN  -  Tutorials  -  Howtos  -  nmPrimer  -  Misc  -  Links  -  Contacts
Last modified: Fri, 22 Jan 2010 11:57:02 +0200

Complete: ##################-- (90%)

Howto: Compiling C++ Programs

In this how to, compilation of C++ sources will be demonstrated using the g++ compiler under Linux. A description of a Windows-based compiler is given in Compiling with the Dev-C++ compiler.

g++ is a free C++ compiler, it is part of the GNU Compiler Collection. If you use the Linux operating system then there is a good chance that g++ is already installed; open a command shell and type

  $ g++ --version
if you get an appropriate response then you have g++ installed [add a link to a g++ install howto].

This short document explains how to perform basic g++ compilations.

First let's say you have a C++ source file called pancake.cpp:

// Output the recipe for pancakes.
#include <iostream>
using namespace std;

int main()
{
  int i;
  cout << "Some milk," << endl;
  cout << "some water," << endl;
  cout << "an egg," << endl;
  cout << "some salt.\n" << endl;
  cout << "Mix and fry." << endl;
  return 0;
}
Some milk,
some walter,
an egg,
some salt.

Mix and fry.

Basic compilation

To compile to an executable called pancake the basic command is:

  $ g++ pancake.cpp -o pancake
This compiles the executable to object code and links additional object codes to form an executable.

The program can be executed by typing pancake or you may need to give the path as follows

  $ ./pancake
Many compiler options are available, they are listed in the gcc manual:
  $ man g++
It is recommended to use the -Wall option which turns on all optional warnings which are desirable for normal code.
  $ g++ -Wall pancake.cpp -o pancake
  pancake.cpp: In function 'int main()':
  pancake.cpp:7: warning: unused variable 'i'
In this case the -Wall option picks up an unused variable; the declaration int i; should be removed from the program.

Compiling multiple sources

Multiple sources can be compiled into a single executable as follows:

  $ g++ powers.cpp power1.cpp power2.cpp power3.cpp -o powers
Here powers.cpp contains the main program, power1.cpp, power2.cpp, and power3.cpp contain function definitions in separate files.
Header files power1.h, power2.h, and power3.h are also present. These sources are shown below.

Main program powers.cpp
// Outputs a value raised to the power of 1, 2, and 3.
#include <iostream>
#include "power1.h"
#include "power2.h"
#include "power3.h"

int main() {
  double x=2.5;
  std::cout << power1(x) << " "
            << power2(x) << " "
            << power3(x) << std::endl;
}
2.5 6.25 15.625

Function power1.cpp and it's header file power1.h
// Returns a value to the power of 1
double power1(double a) {
  return a;
}
#ifndef POWER1_H
#define POWER1_H
// Prototype for function power1()
// returns a value to the power of 1
double power1(double);
#endif

Function power2.cpp and it's header file power2.h
// Returns a value to the power of 2
double power2(double a) {
  return a*a;
}
#ifndef POWER2_H
#define POWER2_H
// Prototype for function power2()
// returns a value to the power of 2
double power2(double);
#endif

Function power3.cpp and it's header file power3.h
// Returns a value to the power of 3
double power3(double a) {
  return a*a*a;
}
#ifndef POWER3_H
#define POWER3_H
// Prototype for function power3()
// returns a value to the power of 3
double power3(double);
#endif

Creating and linking object files

The compilation:

  $ g++ powers.cpp power1.cpp power2.cpp power3.cpp -o powers
Can be replaced with
  $ g++ powers.cpp power1.o power2.o power3.o -o powers
where object files power1.o, power2.o and power3.o are first created with
  $ g++ -c power1.cpp
  $ g++ -c power2.cpp
  $ g++ -c power3.cpp
The -c option means compile the source file, but do not link.
This is useful when some source files are complete and so do not require recompiling while code in other files are being developed.

Creating and linking library files

The object files created in the previous section can be placed into a single library which can then be linked during compilation. This is achieved by using the archive tool ar (see man ar for details).

Object files can be placed in a library for example as follows:

  $ ar rcvf libsubs.a power1.o power2.o power3.o
The library created is called libsubs.a, it contains the three objects sub1.o, sub2.o and sub3.o. Note that the name of the library must begin with lib and end with .a.

More objects can be added to the library with:

  $ ar rcvf libsubs.a another.o

The contents of a library can be listed with:

  $ ar tv libsubs.a

Functions in the library can be listed with:

  $ nm libsubs.a

Objects can be deleted from the library with:

  $ ar dv libsubs.a power1.o

Once a library has been created, it can be linked in a compilation as follows:

  $ g++ powers.cpp -L. -lsubs -o powers

The -L. options tells the linker that the library can be found in the current directory. If the library is not in the current directory then you should give the path, for example:

  $ g++ powers.cpp -L/home/fred/cpp/lib/ -lsubs -o powers
or
  $ g++ powers.cpp -L$MYLIB -lsubs -o powers
where the environment variable MYLIB is set with:
  $ MYLIB=/home/fred/cpp/lib/ 
please send us you comments/questions/corrections