Source code files
Main3.c
1#include <stdio.h>2#include"Static_lib.h"3#include"Fun.h"4 5 intMainvoid){6 intA=2, b=3;7printf"add:%d sub:%d mul:%d div:%d\n", add (A, b), Sub (A, b), Mul ( A, B), Div (A, b));8 fun1 ();9 return 0;Ten}
Static_lib.h and
1 extern int Add (int A, int b); 2 extern int Sub (int A, int b); 3 extern int mul (int A, int b); 4 extern int div (int A, int b);
Static_lib.c
1 intAddintAintb) {2 returnA +b;3 }4 5 intSubintAintb) {6 returnAb;7 }8 9 intMulintAintb) {Ten returnAb; One } A - intDivintAintb) { - returnAb; the}
Fun.h and
1 extern void fun1 ();
Fun.c
1 #include <stdio.h>2void fun1 () {3 printf (" Fun1 ... " ); 4 }
Where the files in STATI_LIB.C are compiled into a static library
Normal compilation
According to the normal compilation method, this program can be compiled in the following order
But it is obviously unwise to compile the source code after each change, so you need to write a makefile rule file
Makefile Base
The rules for Makefile are
Where target is the destination file or action
Prerequisites is a file that generates target dependencies
command is the rule execution of commands, and if the command and the dependency rule are on one line, then the '; ' Split, if on another line, start with tab
Write one of the simplest makefile compile these sources
1 APP3:STATIC_LIB.A FUN.O2GCC main3.c STATIC_LIB.A FUN.O-o app33 4 FUN.O:5GCC-C FUN.C6 STATIC_LIB.O:7GCC-C STATIC_LIB.C8 9 STATIC_LIB.A:STATIC_LIB.OTen ar RCS STATIC_LIB.A static_lib.o One A Clean : -RM STATIC_LIB.A STATIC_LIB.O FUN.O APP3
Do clean to clear the original
Execute make will recompile the source code
So when we modify the source code, we can simply compile the source
Makefile Hidden
But the above makefile is obviously not concise enough, we can use some of the implicit rules of make, shorthand makefile, these implicit rules can be viewed through make-p instructions.
1 app3 : static_lib.a fun.o main3.o2 gcc main3.o fun.o static_lib.a-o app334 STATIC_LIB.A:STATIC_LIB.O # Implicitly will look for *.c files compiled into *. o file 5 ar rcs STATIC_LIB.A static_lib.o67 Clean : 8 rm *.a *.O APP3
Execute make to see that the compilation is still successful
Where CC is a symbolic link
Makefile Var
You can further use some variables to further improve the maintainability of the makefile
1CC =GCC2OBJS =main3.o STATIC_LIB.A FUN.O3gen_opts =-o4 app3: $ (OBJS)5 $ (CC) $ (OBJS) $ (gen_opts) app36 7STATIC_LIB.A:STATIC_LIB.O # Implicitly will look for *.c files compiled into *. o File8 arRCS STATIC_LIB.A STATIC_LIB.O9 Ten Clean : One RM*.A *.O APP3
Execute make
It is also possible to see the 1,2,4 of GCC in the line, indicating that the implied rules are compiled with the specified cc variables.
Makefile Autovar
Further, we can use some of the automation variables.
1CC =GCC2OBJS =main3.o STATIC_LIB.A FUN.O3gen_opts =-o4 app3: $ (OBJS)5 $ (CC) $ (OBJS) $ (gen_opts) app36 7STATIC_LIB.A:STATIC_LIB.O # Implicitly will look for *.c files compiled into *. o File8 arRCS [email protected] $^9 Ten Clean : One RM*.A *.O APP3
where the automation variable [email protected] represents the set of target files in the rule, $^ represents the dependency set, and the makefile can get the desired result after execution.
Makefile Fun
Finally combine the preceding variables, and then combine functions, and so write a more perfect makefile, can do to add a new C file or H file does not need to modify the makefile file, again before, first move static_lib.* to the Lib directory, the remaining source files to the SRC directory.
and modify the include for main3.c to: /lib/static_lib.h, modify the makefile file as follows
1CC =GCC2SRC =./src3LIB =./Lib4OBJS = $ (Patsubst%.c,%.O, $ (wildcard $ (SRC)/*. c))5 LIBS = $ (Patsubst%.c,%.A, $ (wildcard $ (LIB)/*.c))6 gen_opts =-O7 app3: $ (OBJS) $ (LIBS)8 $ (CC) $ (OBJS) $ (LIBS) $ (gen_opts) app39 @echo "Gen Done"Ten # Compile the C file in the Lib directory into a library file One $ (LIBS): $ (Patsubst%.c,%.O, $ (wildcard $ (LIB)/*.c)) A ar RCS [email protected] $^ - - Clean : the -rm $ (SRC)/*.O $ (LIB)/*.a app3 - @echo Clean-done.
Where Patsubst is a function that replaces the wildcard character, the wildcard function expands the wildcard character and expands all matching files under the directory.
The word "-" in front of the command indicates that the execution continues even if an error occurs.
Makefile File Preparation