SCons
scons是一个Python写的自动化构建工具。每次构建时,会自动执行必要的命令以及重建必要的组件(即整个项目如果只修改了一份源代码文件,将仅重新构建跟这份源代码相关的组件)。
默认构建时,将自动查找当前目录是否存在SConstruct、Sconstruct、sconstruct的文件(依次查找、找到就停),然后执行该文件,也可以使用“-f”选项指定构建文件。
示例
假设当前项目需要用到动态库以及C/C++混编,目录结构如下:
src/a.cpp src/a.h src/main.cpp src/mod0: b.cpp b.h src/mod1: c.c c.h
main.cpp
1 #include "a.h" 2 #include "b.h" 3 extern "C" 4 { 5 #include "c.h" 6 } 7 #include <stdio.h> 8 9 int main() 10 { 11 puts("Hello"); 12 A(); 13 B(); 14 C(); 15 return 0; 16 }
a.h
1 #ifndef _a_H_ 2 #define _a_H_ 3 void A(); 4 #endif
a.cpp
1 #include "a.h" 2 #include <stdio.h> 3 void A() 4 { 5 puts("A"); 6 }
mod0/b.h
1 #ifndef _b_H_ 2 #define _b_H_ 3 void B(); 4 #endif
mod0/b.cpp
1 #include "b.h" 2 #include <math.h> 3 #include <stdio.h> 4 void B() 5 { 6 printf("%f\n", log2(1024)); 7 }
mod1/c.h
1 #ifndef _c_H_ 2 #define _c_H_ 3 void C(); 4 #endif
mod1/c.c
1 #include "c.h" 2 #include <stdio.h> 3 void C() 4 { 5 puts("C"); 6 }
只需要将SConstruct如下编写即可
1 #! /usr/bin/env python 2 debug = ARGUMENTS.get('debug', '0') 3 4 cc = 'gcc' 5 ccflags = ['-D_REENTRANT', '-Wall', '-W', '-mavx2', '-O3'] 6 cpppath = ['src/mod1'] 7 CEnv = Environment(CC=cc, CCFLAGS=ccflags, CPPPATH=cpppath) 8 csrc = [] 9 for dirs in cpppath: 10 if int(debug) == 1: 11 obj_path = 'obj_debug/'+dirs 12 else: 13 obj_path = 'obj_release/'+dirs 14 VariantDir(obj_path, dirs, duplicate = 0) 15 now_obj = CEnv.Object(Glob(obj_path+'/*.c')) 16 csrc = csrc + now_obj 17 18 cc = 'g++' 19 ccflags = ['-D_REENTRANT', '-W', '-Werror', '-mavx2', '-std=c++11', '-O3'] 20 cpppath = ['src', 'src/mod0', 'src/mod1'] 21 libs = ['pthread', 'm'] 22 libpath = [] 23 ldflags = [] 24 25 target_name = 'outa-r' 26 if int(debug) == 1: 27 ccflags.append('-g') 28 target_name = 'outa-g' 29 30 env = Environment(CXX=cc, CCFLAGS=ccflags, CPPPATH=cpppath, LIBS=libs, LIBPATH=libpath, LINKFLAGS=ldflags) 31 cppsrc = [] 32 for dirs in cpppath: 33 if int(debug) == 1: 34 obj_path = 'obj_debug/'+dirs 35 else: 36 obj_path = 'obj_release/'+dirs 37 VariantDir(obj_path, dirs, duplicate = 0) 38 now_obj = env.Object(Glob(obj_path+'/*.cpp')) 39 cppsrc = cppsrc + now_obj 40 41 env.Program(target = target_name, source = cppsrc + csrc)
当需要将静态库加入时,只需要修改“Program”函数的‘source“参数即可