Makefile is a configuration file for the program make
, it contains instructions for compiling one or more programs)
Here is an example of C++ Makefile named progC. This program calls Cplex libraries
# Example of makefile For a Cplex in C++ CFLAGS=-Wall -O #CFLAGS=-Wall -g CPPFLAGS=-DNDEBUG -DIL_STD CXX=g++ OBJ=progC.o progC2.o progC: $(OBJ) $(CXX) $(CFLAGS) -o progC $(OBJ) -lm -lilocplex -lconcert -lcplex -lpthread # ^ TAB obligatoire en début de ligne # ----- librairies pour CPLEX ---------- progC.o: progC.cpp $(CXX) $(CFLAGS) $(CPPFLAGS) -c progC.cpp progC2.o: progC2.cpp $(CXX) $(CFLAGS) $(CPPFLAGS) -c progC2.cpp clean: rm -f progC progC.o
hash character (#) indicates that the line is a comments line, and will not be executed.
CFLAGS=-Wall -O
: The variable CFLAGS
will be assigned the value -Wall -O
. Later you can call this variable in your program with the following syntax : $(CFLAGS)
. In this case the variable CFLAG corresponds to Warning and optimization options for the compiler.
The instruction CPPFLAGS=-DNDEBUG -DIL_STD
,is another assigned variable containing other parameter for the C++ preprocessor. in this case, it indicates to the preprocessor to define NDEBUG
and IL_STD
. These definitions are necessary in order for CPLEX to compile.
CXX=g++, assign the g++ compiler. . OBJ=progC.o progC2.o, here we define the variable OBJ which will include progC.o and progC2.o
progC: $(OBJ)
$(CXX) $(CFLAGS) -o progC $(OBJ) -lm -lilocplex -lconcert -lcplex -lpthread
progC.o: progC.cpp
$(CXX) $(CFLAGS) $(CPPFLAGS) -c progC.cpp
progC2.o: progC2.cpp
$(CXX) $(CFLAGS) $(CPPFLAGS) -c progC2.cpp
clean:
rm -f progC progC.o
Source code file : progC.cpp
#include "progC2.h" int main(int argc, char **argv) { int n; if (argc < 2) { printf("Usage: ./progC n \n"); exit(1); } n = atoi(argv[1]); Fibonacci(n); return(1); }
Here is the c++ source code file called progC2.cpp
#include "progC2.h" /* Print the nth number of Fibonacci's suite where n is a user input */ void Fibonacci(int n) { int count, t1=0, t2=1, display=0; printf("Fibonacci Series: %d - %d - ", t1, t2); /* Print the 2 first numbers */ count=2; /* count=2 because the 2 first numbers are already printed */ while (count<n) { display=t1+t2; t1=t2; t2=display; ++count; printf("%d - ",display); } printf("\n"); }
Finally, here is the source code for the header file ,,.h
<file make c++>
#include <cstdio>
#include <cstdlib>
#include <cmath>
void Fibonacci(int n);
</file>
a Makefile file is composed of rules that have the following syntax
Target: dependencies
command
Avant chaque commande il y a un TAB (caractère de tabulation).
To execute the Makefile file, you need to type
make
The standard output when executing
make
>make
g++ -Wall -O -DNDEBUG -DIL_STD -c progC.cpp
g++ -Wall -O -o progC progC.o progC2.o -lm -lilocplex -lconcert -lcplex -lpthread
Here are the files that are called when typing the command
make is called
makefile makefile~ progC progC2.cpp progC2.cpp~ progC2.h progC2.h~ progC2.o progC.cpp progC.cpp~ progC.o
This is how to execute the program
>./progC 14
Fibonacci Series: 0 - 1 - 1 - 2 - 3 - 5 - 8 - 13 - 21 - 34 - 55 - 89 - 144 - 233 -
This how you use
make clean'' command (defined in Makefile)
>make clean rm -f progC progC.o