User Tools

Site Tools


cs:makefile_1
Return to Home page

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs:makefile_1 [2016/07/28 00:02]
cs:makefile_1 [2020/11/26 23:18] (current)
Line 1: Line 1:
 +====== Makefile 1 ======
 +**Concepts:**\\
 +How to write a simple ''makefile''
 +
 +**Text:**\\
 +In the directory ''src'' you can find in the file ''fz.c'' the implementation of the function ''mul'' that returns the results of the multiplication of two integer numbers:
 +<file C src/fz.c>
 +#include "fz.h"
 +
 +int mul(int a, int b){
 +
 +  return a*b;
 +}
 +</file>
 +In the directory ''inc'' you can find in the file ''fz.h'' the prototype of the function ''mul'':
 +<file C inc/fz.h>
 +#ifndef _FZ_H
 +#define _FZ_H
 +
 +int mul(int a, int b);
 +
 +#endif
 +</file>
 +Finally, the ''main.c'' file in the "." directory makes use of the function ''mul'' to obtain the result of the multiplication between numbers 2 and 4:
 +<file C main.c>
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include "fz.h"
 +
 +int main(){
 +
 +  printf("%d\n", mul(2,4));
 +
 +  return EXIT_SUCCESS;
 +}
 +</file>
 +
 +Build a ''makefile'' for the compilation of this program. In particular, the ''makefile'' has to:
 +  * obtain an executable program with name ''prog''
 +  * have a target ''clean'' that removes all object files and the executable ''prog''
 +  * have a target ''install'' that creates a directory ''bin'' and copies the executable ''prog'' in ''bin''
 +  * have a target ''distclean'' that removes the directory ''bin'' and its content, all object files and the executable ''prog''
 +
 +
 +**Example:**\\
 +All files are compiled and the //linker// generates the executable ''prog'':
 +<code bash>
 +$ make
 +gcc -Wall -I inc -g -c main.c
 +gcc -Wall -I inc -g -c src/fz.c
 +gcc -o prog main.o fz.o
 +</code>
 +Modifying one file, for example ''fz.c'' with the command ''touch src/fz.c'', only the modified file is compiled:
 +<code bash>
 +$ touch src/fz.c
 +$ make
 +gcc -Wall -I inc -g -c src/fz.c
 +gcc -o prog main.o fz.o
 +</code>
 +With ''make clean'', the files ''main.o'', ''fz.o'' and ''prog'' are deleted:
 +<code bash>
 +$ make clean
 +rm -f main.o fz.o
 +rm -f prog
 +</code>
 +With ''make install'' the program is compiled and the executable ''prog'' generated (this first step is performed only because the program ''prog'' has been deleted in the previous step), then the directory ''bin'' is created, and finally the file ''prog'' is copied in the directory ''bin'':
 +<code bash>
 +$ make install
 +gcc -Wall -I inc -g -c main.c
 +gcc -Wall -I inc -g -c src/fz.c
 +gcc -o prog main.o fz.o
 +mkdir -p bin
 +cp prog bin
 +</code>
 +With ''make distclean'' the directory ''bin'' and its content, all object files and the executable ''prog'' are removed:
 +<code bash>
 +$ make distclean
 +rm -f main.o fz.o
 +rm -f prog
 +rm -fr bin
 +</code>
 +
 +**Solution:**\\
 +<file make Makefile>
 +target: main.o fz.o
 + gcc -o prog main.o fz.o
 +
 +main.o: main.c inc/fz.h
 + gcc -Wall -I inc -g -c main.c
 +
 +fz.o: src/fz.c inc/fz.h
 + gcc -Wall -I inc -g -c src/fz.c
 +
 +clean:
 + rm -f main.o fz.o
 + rm -f prog
 +
 +install: target
 + mkdir -p bin
 + cp prog bin
 +
 +distclean: clean
 + rm -fr bin
 +</file>
 +
 +DOWNLOAD COMPLETE EXAMPLE: [[http://wiki.altervista.org/_media/cs/makefile_1.zip|makefile_1.zip]]
 +
 +**Comments:**\\
  

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/makefile_1?do=diff&rev=1469656923&difftype=sidebyside