Scons的简单使用

Scons环境搭建

安装python


安装scons

python -m pip install scons

Scons常用参数

#
scons -c   clean
#只显示编译信息,去除多余的打印信息
scons -Q
#保存依赖关系
scons -Q --implicit-cache hello
#强制更新依赖关系
--implicit-deps-changed
#强制使用原先的依赖关系,即使已经改变
--implicit-deps-unchanged

常用内建变量

  • LIBS 要连接的库文件
  • LIBPATH 库文件路径
  • CCFLAGS 编译标志
  • CPPPATH 头文件
Program('hello.c', CPPPATH = ['include', '/home/project/inc'])

创建环境变量

#根据不同系统创建不同的编译环境变量
env = Environment()
env.Program('foo.c')
#修改环境变量
env = Environment(CC = 'gcc',CCFLAGS = '-O2')

复制环境变量

env = Environment(CC = 'gcc')
opt = env.Copy(CCFLAGS = '-O2')
dbg = env.Copy(CCFLAGS = '-g')

获取环境变量

env = Environment()
print "CC is:", env['CC']

扩展环境变量

env = Environment()
print "CC is:", env.subst('$CC')

替换环境变量

env = Environment(CCFLAGS = '-DDEFINE1')
env.Replace(CCFLAGS = '-DDEFINE2')
env.Program('foo.c')

尾部增加环境变量值

env = Environment(CCFLAGS = '-DMY_VALUE')
env.Append(CCFLAGS = ' -DLAST')
env.Program('foo.c')

头部添加环境变量值

env = Environment(CCFLAGS = '-DMY_VALUE')
env.Prepend(CCFLAGS = '-DFIRST ')
env.Program('foo.c')

编译可执行程序

#编译hello.c可执行文件
#根据系统自动生成(hello.exe on Windows; hello on POSIX)
Program('hello.c')
#指定Output文件名
Program('new_hello','hello.c')
# 编译多个文件,Output文件名以第一个文件命名
Program(['main.c', 'file1.c', 'file2.c'])
Program('program', Split('main.c file1.c file2.c'))
Program('hello',Glob("*.c"))
Program(‘helloscons2’, [‘helloscons2.c’, ‘file1.c’, ‘file2.c’],
	LIBS = ‘m’,
	LIBPATH = [‘/usr/lib’, ‘/usr/local/lib’],
	CCFLAGS = ‘-DHELLOSCONS’)

多个工程共享源文件

#把共同的文件列表单独提取出来,以便维护
common = [‘common1.c’, ‘common2.c’] 
foo_files = [‘foo.c’] + common
bar_files = [‘bar1.c’, ‘bar2.c’] + common
Program(‘foo’, foo_files)
Program(‘bar’, bar_files)

编译二进制文件

#编译hello.c目标文件
Object('hello.c')

编译库文件

#编译library
Library('foo', ['f1.c', 'f2.c', 'f3.c'])
#编译 static library
SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
#编译 shared library
StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])

链接库编译

#连接库,不需加后缀或是前缀
Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.')

判断源文件是否修改

#根据内容是否改变,默认方式
SourceSignatures('MD5')
#根据修改时间,类似makefile
SourceSignatures('timestamp')

判断目标文件是否改变

#根据编译结果
TargetSignatures('build')
#根据文件内容,如果只是加了句注释,将不会被重新编译
TargetSignatures('content')

文件依赖

#忽略某个依赖关系
Ignore(hello, 'hello.h')
#明确依赖关系
Depends(hello, 'other_file')
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值