声明性格式是指声明程序的意图/最终结果,而不是它应该如何到达的格式.本质上,它是配置文件和一段代码之间的区别.相比:
CC=gcc
CFLAGS=-I.
DEPS = hellomake.h
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
hellomake: hellomake.o hellofunc.o
gcc -o hellomake hellomake.o hellofunc.o -I.
Run Code Online (Sandbox Code Playgroud)
与(python-pseudocode)类似的东西:
CC = "gcc"
CFLAGS = "-I."
DEPS = ["hellomake.h"]
def make_o_file(o_file, deps):
#don't recompile if it exists & is current
c_file = "%s.c" % os.path.splitext(o_file)[0]
if (os.path.exists(o_file)
and is_newer(o_file, c_file)
and all(is_newer(o_file, dep) for dep in deps)):
return
#else actually compile it
compile_c_file(CC, code_file, o_file, CFLAGS)
if target == "hellomake":
make_o_file("hellomake.o", DEPS)
make_o_file("hellofunc.o", DEPS)
link_o_files("hellomake", ["hellomake.o", "hellofunc.o"])
Run Code Online (Sandbox Code Playgroud)
如果格式设计合理,前者可以更容易地处理.关于声明性编程的维基百科可能很有用.