brief note on Makefile

来源:互联网 发布:电视盒子无法安软件 编辑:程序博客网 时间:2024/06/05 22:33

usage scene:

compile source code on Unix

auto compilation of the whole project


content:

instructions (like shell)


command “make”:

compile (+pack) + link


file "makefile":

tell command "make" how to compile & link 


brief flow:

1) if the project has not been compiled before, compile all of the source files and link the target program

2) if it is compiled before and there's some files modified, compile the modified ones and link the target program

3) if the head file is modified, recompile the files that refer to it and relink the target program


A Sample(from wiki.ubuntu):


edit : main.o kbd.o command.o display.o \insert.o search.o files.o utils.o       /*comments:if the .o files are modified then the following command will launch*/cc -o edit main.o kbd.o command.o display.o \insert.o search.o files.o utils.omain.o : main.c defs.hcc -c main.ckbd.o : kbd.c defs.h command.hcc -c kbd.ccommand.o : command.c defs.h command.hcc -c command.cdisplay.o : display.c defs.h buffer.hcc -c display.cinsert.o : insert.c defs.h buffer.hcc -c insert.csearch.o : search.c defs.h buffer.hcc -c search.cfiles.o : files.c defs.h buffer.h command.hcc -c files.cutils.o : utils.c defs.hcc -c utils.cclean :rm edit main.o kbd.o command.o display.o \insert.o search.o files.o utils.o


how dose make work:

1. find the file with the name "Makefile" or "makefile" in current folder

2. if found, make process will find the first target as the final target (in the sample code above, the target is file "edit")

3. if the file edit dose not exit, or the .o files are modified after they were linked, then make will execute the following shell code to emerge the file edit

4. if the .o files that file edit depends on dose not exit too, find the lines that describe it and run the command

5.  so, at the beginning, make will create the .o files and then links them to be the final target 

6. ADDITION: if the file, like file clean, does not have connections with other files, we can put it in an explicit command like "make clean" to do some special functions


parameter in makefile(sample comes from wiki.ubuntu):

objects = main.o kbd.o command.o display.o \insert.o search.o files.o utils.oedit : $(objects)cc -o edit $(objects)



let make auto emerge(default feature):


objects = main.o kbd.o command.o display.o \insert.o search.o files.o utils.o cc = gccedit : $(objects)cc -o edit $(objects)main.o : defs.hkbd.o : defs.h command.hcommand.o : defs.h command.hdisplay.o : defs.h buffer.hinsert.o : defs.h buffer.hsearch.o : defs.h buffer.hfiles.o : defs.h buffer.h command.hutils.o : defs.h.PHONY : clean  /*to show that the clean is a fake target*/clean :rm edit $(objects)

make can let you collect the .h and .c files together, however, i don't like it, so let it go~O(∩_∩)O~



everthing above comes from wiki.ubuntu


0 0
原创粉丝点击