User Tools

Site Tools


cs:makefile_1
Return to Home page

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:

src/fz.c
#include "fz.h"
 
int mul(int a, int b){
 
  return a*b;
}

In the directory inc you can find in the file fz.h the prototype of the function mul:

inc/fz.h
#ifndef _FZ_H
#define _FZ_H
 
int mul(int a, int b);
 
#endif

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:

main.c
#include <stdio.h>
#include <stdlib.h>
#include "fz.h"
 
int main(){
 
  printf("%d\n", mul(2,4));
 
  return EXIT_SUCCESS;
}

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:

$ 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

Modifying one file, for example fz.c with the command touch src/fz.c, only the modified file is compiled:

$ touch src/fz.c
$ make
gcc -Wall -I inc -g -c src/fz.c
gcc -o prog main.o fz.o

With make clean, the files main.o, fz.o and prog are deleted:

$ make clean
rm -f main.o fz.o
rm -f prog

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:

$ 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

With make distclean the directory bin and its content, all object files and the executable prog are removed:

$ make distclean
rm -f main.o fz.o
rm -f prog
rm -fr bin

Solution:

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

DOWNLOAD COMPLETE EXAMPLE: 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
/web/htdocs/www.skenz.it/home/data/pages/cs/makefile_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki
Privacy Policy