Makefile instance
[Reprint, a very good blog post, step by step, easy to understand]
Suppose we have the following program, the source code is as follows:
/* Main. C */
# Include <mytool1.h>
# Include <mytool2.h>
Int main (INT argc, char ** argv)
{
Mytoolpattern print ("hello ");
Mytool2_print ("hello ");
}
/* Mytool1.h */
# Ifndef _ mytool_1_h
# DEFINE _ mytool_1_h
Void mytooltypesprint (char * print_str );
# Endif
/* Mytool1.c */
# Include <mytool1.h>
Void mytooltypesprint (char * print_str)
{
Printf ("this is mytool1 print % s \ n", print_str );
}
/* Mytool2.h */
# Ifndef _ mytool_2_h
# DEFINE _ mytool_2_h
Void mytool2_print (char * print_str );
# Endif
/* Mytool2.c */
# Include <mytool2.h>
Void mytool2_print (char * print_str)
{
Printf ("this is mytool2 print % s \ n", print_str );
}
Of course, because this program is very short, we can compile it like this.
Gcc-C main. C-I.
Gcc-C mytool1.c-I.
Gcc-C mytool2.c-I.
Gcc-O main. O mytool1.o mytool2.o
Because we use our own two header files in the program, and when we include these two header files, we use <> this way, the compiler will go to the system's default header file path during compilation to find our 2 header files. Because our 2 header files are not in the system's default path, therefore, we can expand the default path of the system by ourselves, so we use-I. option to extend the system's default header file path to the current path.
In this way, we can also generate the main program, which is not very troublesome. however, if one day we modified one of the files (for example, mytool1.c), do we have to re-enter the above command? You may say that this is easy to solve. I wrote a shell script and asked her to help me complete it. yes, it can play a role for this program. but when we want to make things more complex, if our program has hundreds of source programs, should the compiler recompile them one by one?
For this reason, smart programmers have come up with a good tool to do this. This is make. we only need to execute make to solve the above problem. before executing make, we need to compile a very important file. -- makefile. for the above program, a possible MAKEFILE file is:
# This is the MAKEFILE file of the above program.
Main: Main. O mytool1.o mytool2.o
Gcc-O main. O mytool1.o mytool2.o
Main. O: Main. c mytool1.h mytool2.h
Gcc-C main. C-I.
Mytool1.o: mytool1.c mytool1.h
Gcc-C mytool1.c-I.
Mytool2.o: mytool2.c mytool2.h
Gcc-C mytool2.c-I.
With this makefile, no matter when we modify any file in the source program, we only need to execute the make command, our compilers only compile the files related to the files we modified. She doesn't even want to handle other files.
Next we will learn how to compile makefile.
In makefile, # the start line is a comment line. The most important thing in makefile is to describe the dependency of the file. The general format is:
Target: Components
Tab rule
The first line indicates dependency, and the second line indicates rules.
Main: Main. O mytool1.o mytool2.o
The dependent object (components) of Target main is main. O mytool1.o mytool2.o when the dependent object is modified after the target is modified, it is necessary to execute the command specified by the rule line. as mentioned in the third line of makefile above, we need to execute gcc-O main. O mytool1.o mytool2.o note that the tab in the Rule line indicates that there is a tab key.
Makefile has three very useful variables: $ @, $ ^, and $ <, which indicate the following meanings:
$ @ -- Target file, $ ^ -- all dependent files, $ <-- the first dependent file.
If we use the above three variables, we can simplify our MAKEFILE file:
# This is the simplified makefile.
# Do you remember our optimization and warning options?
Main: Main. O mytool1.o mytool2.o
Gcc-O2-o $ @ $ ^
Main. O: Main. c mytool1.h mytool2.h
Gcc-O2-wall-C $ <-I.
Mytool1.o: mytool1.c mytool1.h
Gcc-O2-wall-C $ <-I.
Mytool2.o: mytool2.c mytool2.h
Gcc-O2-wall-C $ <-I.
After the simplification, our makefile is a little simpler, but sometimes people want to make it simpler (alas, it seems that programmers are all lazy). For this reason, make has some default rules.
Here we will learn the default rule of a makefile.
. C. O:
Gcc-C $ <
This rule indicates that all. O files are dependent on their corresponding. c files. For example, if mytool. O depends on mytool. C, makefile can also be changed:
# This is the simplified makefile.
Main: Main. O mytool1.o mytool2.o
Gcc-o $ @ $ ^
. C. O:
Gcc-C $ <-I.
If you still think this is too troublesome, we can simplify the work. for this reason, let's take a look at some default macro definitions in makefile. makefile generally has many macros. Here we just want to learn a few commonly used macros.
CC indicates our compiler name. The default value is CC.
Cflags indicates the compiler options we want to compile.
Ldlibs indicates the Connection Library option of the compiler during compilation. (this option is not available in our program)
After the above macro is used, our makefile can be
Cc = gcc
Cflags =-g-wall-O2-I.
Main: Main. O mytool1.o mytool2.o
. C. O:
CC cflags $ <
Now, we have almost finished makefile. You can input make-F makefile to run the MAKEFILE file. For more information about makefile rules, see relevant documents. Use (man make or info make)
In fact, you can create makefile for a small project. For a large project, it is best to use tools such as automake, which is quite convenient.
From: http://hi.baidu.com/liuzhiliangliang/blog/item/52a1f13d05abdcc19e3d6277.html