0. Preface from learning C language began to slowly began to contact Makefile, consulted a lot of makefile information but always feel no real master makefile, if you write a makefile always feel very laborious. So deliberately using the blog to summarize the relevant knowledge of makefile, through examples to illustrate the specific use of makefile. For example, makefile is divided into the following sections, for more information please refer to "Example Makefile Index blog post" 1. Only a single C file 2. Contains multiple C files 3. You need to include the header file path 4. Add a macro definition 5. Add a system Shared library 6. Add customizing shared Libraries 7. A practical example of the "code warehouse"--makefile-example code warehouse is located in BitBucket, which can be cloned with the help of Tortoisehg (GUI tools) or download the zip package directly in the Web page. 1. Three C files and three header files The example here is slightly more complicated but closer to the actual situation. The file results are as follows: The root directory contains test.c Makefileh and folder Test-add and folder Test-sub. TEST.c makefile "Test-add" test-add.c test-add.h "test-sub" TEST-SUB.C test-sub.h "test.c"
[CPP]View PlainCopy
- #include <stdio.h>
- #include <test-add.h>
- #include <test-sub.h>
- int main (void)
- {
- int a = 3;
- int b = 2;
- printf ("a=%d\n", a);
- printf ("b=%d\n", b);
- printf ("a+b=%d\n", add (b));
- printf ("a-b=%d\n", Sub (b));
- return 0;
- }
"TEST-ADD.C"
[CPP]View PlainCopy
- #include <test-add.h>
- int Add (int A, int b)
- {
- return a+b;
- }
"Test-add.h"
[CPP]View PlainCopy
- #ifndef __test_add
- int Add (int A, int b);
- #endif
"TEST-SUB.C"
[CPP]View PlainCopy
- #include "Test-sub.h"
- int sub (int A, int b)
- {
- return a-B;
- }
"Test-sub.h"
[CPP]View PlainCopy
- #ifndef __test_sub
- int sub (int A, int b);
- #endif
2. Review GCC directives The GCC directives specify the header file path through the-I prefix, specifically./represents the current path,.. /Represents the previous level of the directory. 3. Write makefile replace the [tab] in it and use the makefile file in the code warehouse as the main.
[CPP]View PlainCopy
- # instruction compiler and options
- cc=gcc
- Cflags=-wall-std=gnu99
- # destination File
- Target=test
- SRCs = test.c \
- ./TEST-ADD/TEST-ADD.C \
- ./TEST-SUB/TEST-SUB.C
- INC =-i./test-add-i./test-sub
- OBJS = $ (SRCS:.C=.O)
- $ (TARGET): $ (OBJS)
- # @echo Target:[email protected]
- # @echo objects:$^
- [Tab]$ (CC)-o [email protected] $^
- Clean
- [Tab]rm-rf $ (TARGET) $ (OBJS)
- %.o:%.c
- [Tab]$ (CC) $ (CFLAGS) $ (INC)-o [email protected]-C $<
"specific" "1" The header file path is developed through the variable Inc, compared to the makefile of a single file and multiple files. The header file path is separated by a space. "2" compilation rule%.o:%.c added header file parameter $ (CC) $ (CFLAGS) $ (INC) -o [email protected]-C $< The Gcc-wall-std=gnu99 -i./test-add-i./test-sub -o test.o-c test.c will appear in the process of compiling. The header file path parameter is added compared to the makefile of a single file and multiple files. "3" SRCs variable, the "\" symbol can be used to continue the line when the file is large. compile make clean && make "console output" RM-RF test TEST.O./test- ADD/TEST-ADD.O./test-sub/test-sub.ogcc-wall-std=gnu99 -i./test-add-i./test-sub -o test.o-c TEST.CGCC- Wall-std=gnu99 -i./test-add-i./test-sub -o test-add/test-add.o-c test-add/test-add.cgcc-wall-std=gnu99 -i./test-add-i./test-sub -o test-sub/test-sub.o-c test-sub/test-sub.cgcc-o Test TEST.O test-add/ TEST-ADD.O test-sub/test-sub.o The output from the console shows that the last executable and target files are cleared by make clean, and then each C file is compiled in turn, and the header file path is established during the compilation process. Finally, the 3 target files are linked to the final executable file.
[Go]linux Learning Note--Makefile header file Find path