Makefile and makefile
At first, I will intercept a piece of nonsense about how to learn makefile. If you don't want to listen, please jump directly to my makefile tutorial.
First of all, we must first explain that learning makefile is really a painful process, especially the process of reading and learning through dry reading. It can be simply described as a question ...... But what does it mean to be a real programmer? In order not to be criticized for programming, you only use IDE. You have to learn this painful thing with your head. Fortunately, there is a sentence, pain is the identifier of progress, this at least shows that you are making progress and it is quite good.
Through these days of hard work, I think the steps for learning makefile scores are as follows:
1. Familiar with makefile statements and writing a few simple makefiles-simply compile a demo program that translates several files, which can greatly improve your self-confidence.-done
2. Read chapter 1, 2, 3, 6, and 8 of the GNU Make project management book.-done
3. Then write the makefile of a larger system.-done
4. goto 2;-todo
5. Remind yourself to use makefile for management in normal projects, and constantly search for new knowledge points, records, and reviews.-todo
I have to say that after completing step 1, I do not feel like I have learned makefile, but I still cannot write too many makefiles. I can only say that I am no longer afraid of large makefiles, I am confident that I can understand the compilation system that this makefile is intended. This is also an entry to makefile.
--------------------------
When I started to complete the above step 3rd, I still did not know anything. The most painful thing is that I did not know how to start, and I did not find the "rope head" in the complicated dilemma ", one thing I could think of at the time was google. I used the Keyword: the best way of wrighting makefile, and then I got the address. A very important piece of information here is to avoid the traditional nested makefile. How can we build a large project? I have found this article Recursive make considered harmful, which clearly describes that I have checked a lot of times of being referenced. For details about this article, refer to here, I will not go into details.
After reading this paper, I began to rethink how to complete step 3 above, and then slowly tried to compile one of the static libraries. Finally, I wrote this example:
# include some common definitions of make
include common.mk
MODULES: = asyn_frame rsa platform torrent_parser utility
CFLAGS + = $ (patsubst%,-I%, $ (MODULES))
CFLAGS + = -I. -I./include
CFLAGS + = -DLINUX -DNDEBUG
CFLAGS + = -O2
SRC: = $ (wildcard * .c)
# It's used for debug makefile.
MODULES_MK: = $ (patsubst%, src /% / module.mk, $ (MODULES))
include $ (MODULES_MK)
OBJ: = $ (patsubst% .c,%. O, $ (SRC))
LIB: = libcommon.a
$ (LIB): $ (OBJ)
@echo 'enter produce the lib files *************'
$ (AR) $ (LIB) $ (OBJ)
$ (RM) $ (OBJ)
@echo 'leave produce the lib files *************'
all: $ (LIB)
% d:% c
@set -e; $ (RM) $ @; $ (CC) -M $ (CFLAGS) $ <> $ @. $$$$; \
sed 's, \ ($ * \) \. o [:] *, \ 1.o $ @:, g' <$ @. $$$$> $ @; $ (RM) $ @. $$$ $
-include $ (SRC: .c = .d)
.PHONY: clean all
test:
@echo 'make test enter'
@echo 'the src of asyn_frame:'
@echo $ (SRC)
@echo 'the obj of asyn_frame:'
@echo $ (OBJ)
@echo 'the mk file of common'
@echo $ (MODULES)
@echo $ (MODULES_MK)
@echo $ (CFLAGS)
@echo 'make test leaving'
clean:
$ (RM) $ (LIB) $ (OBJ)
find. -name "* .d" | xargs rm -f
It doesn't look complicated, but the process from scratch is quite tortuous. You can see that there is a pseudo-target test. This is what I used to test the basic variables in make. Constantly trying to finally come out this is ok Usable things. So I finally completed step 3, of course, I was very comfortable in the expected time, at least I can say that there is another weapon in my knowledge base.
After step 3, I did n’t continue to step 4, because I do n’t think I have reached the point where I need to be very familiar with makefiles. The original intention of learning it is also to help myself do practical work in daily work. If the project is too immersed in theory, it is contrary to my original intention. It is not what I want. Moreover, most of the current projects are cross-platform, so CMake is my dish. At the moment, I choose "shallow taste", but I still reviewed the basic knowledge of makefile. My reference is Chen Hao's "Writing Makefile with Me", which is more than 14 chapters. I think this series is not Very good, many things are excerpts from the GNU manual, and of course the author's own understanding, but as a reference to help review some common knowledge points is still very helpful. That's why I didn't find that big and complete textbook, but read this simplified tutorial online.
The front is basically a simple process of learning makefiles. It takes about 3 weeks. After all, I have worked and have children. I really do n’t have much time for myself, but with this thing, I get a good The habit is to get up at around 5:30 every morning and read the book for two hours before going to work. This is really the result of inadvertently inserting willows, because there is really no time to do these things at night. . . Although there is a lot of nonsense in the front, I think that learning knowledge is not the most critical. Understanding how to acquire knowledge is important and usually the most difficult. I tried to describe this thinking process of acquiring knowledge in relatively simple language. After careful reading, I still feel verbose. . Hey, that's it, it's the same thing anyway, and I don't want to do anything afterwards.
Below I will turn to the text and write a tutorial of my own makefile:
table of Contents:
1. Best practice for building makefiles with multi-level directory structure — The Best Way
2. Makefile and explanation of a real project
3. The most common knowledge points of makefile
4. Reference materials
For specific content, please move here "My Makefile Tutorial".
What is makefile / Makefile? What are the characteristics of the file name?
Compiled files under Linux written by yourself. Its role is as follows:
Makefile is related to the compilation rules of the entire project. The source files in a project are not counted, and they are placed in several directories according to type, function, and module. Compile, even for more complex functional operations, because the makefile is like a Shell script, which can also execute operating system commands.
The benefit of makefile is-"automatic compilation", once written, only one make command is needed, the entire project is completely compiled automatically, which greatly improves the efficiency of software development
Reference material: how to Makefile
The meaning of a makefile
See << write me makefile >>
Table 13-2 Main predefined variables of GNU make
Predefined variables
meaning
$ *
The name of the target file without the extension.
$ +
All dependent files, separated by spaces and in the order of appearance, may contain duplicate dependent files.
$ <
The name of the first dependent file.
$?
All dependent files are separated by spaces. The modification date of these dependent files is later than the target creation date.
$ @
The full name of the goal.
$ ^
All dependent files are separated by spaces and do not contain duplicate dependent files.
$%
If the target is an archive member, this variable represents the archive member name of the target. For example, if the target name is
mytarget.so (image.o), then $ @ is mytarget.so, and $% is image.o.
AR
The name of the archive maintenance program. The default value is ar.
ARFLAGS
Options for archive maintenance procedures.
AS
The name of the assembler. The default value is as.
ASFLAGS
Assembler options.
CC
The name of the C compiler. The default value is cc.
CFLAGS
C compiler options.
CPP
The name of the C precompiler. The default value is $ (CC) -E.
CPPFLAGS
C pre-compiled options.
CXX
The name of the C ++ compiler. The default value is g ++.
CXXFLAGS
C ++ compiler options.
FC
The name of the FORTRAN compiler. The default value is f77.
FFLAGS
FORTRAN compiler options.