1. First, take a look at the simplest C file // hello. c file # include void main () {printf ("hello world \ n");} is hello. c. Compile the makefile file, which is compiled using gcc.
- $ Vi makefile
- Hello: hello. c
- Gcc-o hello. c
We can see that the simplest makefile only needs two lines. We can see row 1st: hello is the file to be generated, hello. c is the source file required for compilation, separated by: 2nd lines in the middle: is the specific compilation command 2. We can also use gcc to put hello first. c is compiled into a machine language. o, and then link each file to generate the makefile file above the binary file, which can be written as follows:
- Hello: hello. o
- Gcc-o hello. o
- Hello. o: hello. c
- Gcc-c hello. c
From the code above, we can see that the last compiled hello is by hello. o The Last generated line 3, 4: 3rd, hello. c compile and generate hello. o, Row 3: Generate hello. o For specific commands, see line 1 and line 2: Line 2. the binary file hello requires hello. o, line 3, is it easy to generate the specific hello command. 3. If you need to link multiple files, you only need to generate them. o file, and finally generate the final file such as: file1.h, file1.c, file2.h, file2.c, main. c five files
The makefile file is as follows:
- Main: main. o file1.o file2.o
- Gcc-o main. o file1.o file2.o
- Main. o: main. c file1.h file2.h
- Gcc-c main. c
- File1.o: file1.h file1.c
- Gcc-c file1.c
- File2.o: file2.h file2.c
- Gcc-c file2.c
From the code above, we can see that we need to compile the file1.o, file2.o, and main. o files, and finally generate the final main file through the link.