Today, I learned how to write makefile in C language programming in Linux. This is just a simple example:
Assume that the following file is in the practice Directory: Main. c hello1.c hello2.c hello1.h hello2.h several program files, to compile and connect them, the best way is to write a MAKEFILE file and run it with the make command. Make is a command tool that explains commands in makefile. Once the MAKEFILE file is written, you only need a make command to automatically compile the entire project, greatly improving the efficiency of software development. The MAKEFILE file in this example is as follows:
Main: Main. O hello1.o hello2.o
Gcc-O main. O hello1.o hello2.o (there is a tab key in front)
Main.0: Main. c hello1.h hello2.h
Gcc-C main. c
Hello1.o: hello1.c hello1.h
Gcc-C hello1.c
Hello2.o: hello2.c hello2.h
Gcc-C hello2.c
Clean:
Rm main. O hello1.o hello2.o
At this time, you only need a make command to generate the final result: the executable file main
Note:
1. to regenerate the target code, run the "make clean" command.
2. A more robust clean statement is as follows:
. Phony: clean
Clean:
-Rm main. O hello1.o hello2.o
3. Several target files can also be written as variables, namely:
At the beginning of the MAKEFILE file, write: Objects = main. O hello1.o hello2.c. You can use "$ (objects) to replace it elsewhere!