==========================Makefile==========================
By defining target and rules, it is easier to understand the structure of program. Especially when the program is composed of several files. When there is only few file is modified, we don't need to re-compile all file.
- Basic structure:
# This is comment !!
[Target] : [Dependency] # when generating target, it will check dependency first
[Command] # like g++ -o a.out hello.cpp ...
# notice that a TAB is before command instead of space
- PHONY: 偽目標 (all/ clean/...)
There is no target file to generate, just execute the command
常見的例子:
all : prog1 prog2 prog3
.PHONY : clean all clean : rm *.o temp
prog1 : prog1.o utils.o cc -o prog1 prog1.o utils.o prog2 : prog2.o cc -o prog2 prog2.o prog3 : prog3.o sort.o utils.o cc -o prog3 prog3.o sort.o utils.o
ps. we can add any command we want:
clean : cat main.c rm -f *.o
- Variable declare:
obj = a.o b.o c.o # Declare
program : $(objects) cc -o program $(objects)
- Common symbol:
wildcard character( 萬用字元)
* : 任意長度任意字元
?: 單一長度任意字元
~: ~/test = home/test
~alumi5566/test = home/use/alumi5566/test
- Other symbol
https://blog.csdn.net/liangkaiming/article/details/6267357
wildcard: 擴展通配符號
notdir:去除路徑符號
patsubst: 替換通配符號
src=$(wildcard *.c ./sub/*.c) => src = a.c b.c ./sub/sa.c ./sub/sb.c 路徑下的*.c
dir=$(notdir $(src)) => a.c b.c sa.c sb.c 把路徑去掉
obj=$(patsubst %.c,%.o,$(dir) ) => a.o b.o sa.o sb.o 把dir中的*.c都換成*.o
patsubst 的快速寫法:obj=$(dir:%.c=%.o)
常見的例子:一次把全部的*.c編譯成*.o
objects := $(patsubst %.c,%.o,$(wildcard *.c))
foo : $(objects)
cc -o foo $(objects)
==========================SCONS==========================SConstruct is like.makefile, put it into same folder with source files. Notice that scons is run under python, you need to install python environment before you use it.
- Execute command:
Scons -c: make clean
Scons -Q: Scons without logs
- Basic context in SConstruct
Program("test1.c") # compile test1.c generate object file test1
Program('test', ['test1.c','test2.c'])
#
compile test1.c & test2.c, generate object file test1
Program('test', ['test1.cpp'],LIBS=['boost_system','boost_thread-mt'], LIBPATH='/usr/lib64')
# link library
Library('libtest1',['test1.c'])
# compile to library
- See following usage when I take UCR graphic course, more detail in [5]
import os env = Environment(CC = 'gcc',CCFLAGS = '-O2') env.Program('foo.c')
Object builder:
opt = Environment(CCFLAGS = '-O2') dbg = Environment(CCFLAGS = '-g') o = opt.Object('foo-opt', 'foo.c') opt.Program(o)
The Append
Method:
env = Environment(CCFLAGS = ['-DMY_VALUE']) env.Append(CCFLAGS = ['-DLAST']) env.Program('foo.c')
Reference:
[1] Mr. opengate
http://mropengate.blogspot.com/2018/01/makefile.html
[2] Kito
https://kitoslab.blogspot.com/2011/12/makefile.html
[3] 書寫規則 (這個很詳細)
http://wiki.ubuntu.org.cn/%E8%B7%9F%E6%88%91%E4%B8%80%E8%B5%B7%E5%86%99Makefile:%E4%B9%A6%E5%86%99%E8%A7%84%E5%88%99
[4] Scons basic
http://www.cnblogs.com/MikeZhang/archive/2013/05/22/sconsPrimer_20130522.html
[5] Scons using env
http://scons.org/doc/1.0.1/HTML/scons-user/x1392.html
沒有留言:
張貼留言