Skip to content

C++

Compilation

The default compiler is GCC which includes the gcc program for C and g++ for C++.

If necessary, you can load the gcc module to have a different version from the one included with the system. For example:

module load gcc

It is possible that these version might have unforeseen problems in some circumstances so we recommend that you stick to the default compiler.

Compilation options

Option Description
-O Optimiser the program.
-g Debug the program
-Wall Get more warning when compiling.
-o program_name Name of the program we want to create
-c To create only the object files (.o)
-I dir Add non standard include direcrory
-L dir Add non standard library directory
-lname Link with a library

Examples

If your program is contained in a single file, you can use this command to build an optimized program.

gcc -O -Wall -o hello hello.c

If you need to debug the program, replace -O with -g.

If your program has more than one file, usually objects files are created first and then they are linked together. It's what is done in most Makefile.

gcc -O -Wall -c hello1.c
gcc -O -Wall -c hello2.c
gcc -O -o hello hello1.o hello2.o

As you can see, we're using the -c option to compile a .c to an object file .o.

If you have a C++ program, replace gcc by g++

g++ -O -Wall -c hello1.c
g++ -O -Wall -c hello2.c
g++ -O -o hello hello1.o hello2.o

Compile a program with the cplex library.

gcc -O -Wall -DNDEBUG -DIL_STD -c lpex1.c
gcc -O -o lpex1 lpex1.o -lcplex -lm -lpthread

Compile a C++ program with cplex and concert.

g++ -O -Wall -DNDEBUG -DIL_STD -c cutstock.cpp
g++ -O -o cutstock cutstock.o -lilocplex -lconcert -lcplex -lm -lpthread

Compile a C++ program using gurobi

module load gurobi
g++ -o sudoku sudoku_c++.cpp -lgurobi_g++5.2 -lgurobi100