User Tools

Site Tools


en:programmation-c_c

How to compile a C/C++ program

The default compiler for c programs is gcc and g++ for C++ programs.

If necessary, it is possible to load the module gcc if you need a different version of the compiler.

module load gcc

In order to compile a program using a cplex library, you need to load the modulecplex-studio

module load cplex-studio 

Compiler Options

Option
-O sets the compiler's optimization level
-g Turns on debugging information
-Wall Activates all possible Warnings when compiling your program.
-o file Place output in file file
-c compiles source files without linking. only objet files (.o) gets created
-Idir Adds dir to the list of directories searched for header files
-Ldir Adds dir to the list of directories searched for Library files
-lname Searches the library name specified when linking

Examples

Example 1

If you program have a unique file, you can use the following command to create an optimized program

gcc -O -Wall -o hello hello.c

If you need to debug your program replace « -O » option bug « -g » option.

Example 2

If the program has more than one file it is custom to create objets files then assemble the program. It is the way used to create a Makefile.

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

Example 3

For C++ program need to change the command gcc with g++

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

Example 4

Compiling a C program using Cplex.

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

Example 5

Compiling a C++ using Cplex and Concert.

g++ -O -Wall -DNDEBUG -DIL_STD -c cutstock.cpp
g++ -O -o cutstock cutstock.o -lilocplex -lconcert -lcplex -lm -lpthread
en/programmation-c_c.txt · Last modified: 2019/05/03 18:12 by admin