Skip to content

Makefile

:material-circle-edit-outline: 约 68 个字 :fontawesome-solid-code: 19 行代码

The Makefile utility

  • Targets
    1. Good division to components
    2. Minimum compilation when something is changed
    3. Easy maintenance of project structure, dependencies and creation

Note that the Makefile mechanism is not limited to C programs

Project structure

Project structure and dependencies can be represented as a DAG (= Directed Acyclic Graph)

e.g

– Program contains 3 files

– main.c., sum.c, sum.h

– sum.h included in both .c files

– Executable should be the file sum

image-20240426125212956

sum: main.o sum.o
gcc –o  sum main.o  sum.o

main.o: main.c sum.h  gccc main.c

sum.o: sum.c sum.h  gccc sum.c


//Equivalent makefiles
//.o depends (by default) on corresponding  .c  file.
sum: main.o sum.o
gcc –o sum main.o sum.o

main.o: sum.h  gccc main.c

sum.o: sum.h  gccc sum.c

image-20240426125340238

sum: main.o sum.o
gcc –o  $@  main.o  sum.o
main.o  sum.o: sum.h  gccc $*.c