好记性不如铅笔头

编程

scons初步学习笔记

最近项目上用到了scons,这里笔记下学习记录吧。
百度百科中对于scons的介绍如下:
scons是一个Python写的自动化构建工具,从构建这个角度说,它跟GNU make是同一类的工具。它是一种改进,并跨平台的gnu make替代工具,其集成功能类似于autoconf/automake 。scons是一个更简便,更可靠,更高效的编译软件。
简单来说,scons是一个跨平台的自动化构建工具,使用python编写,可以非常方便的在不同的平台间进行移植。相对于其他的跨平台编译工具,scons使用python编写,可读性和可维护性更好,更容易学习。

scons官方网站:http://scons.org/

根据scons的官方介绍,scons一共有3中获取方式:
1 安装版SCons Packages
这种版本类似于windows上的常用的安装版本。
2 绿色版scons-local Packages
这种版本类似于windows上的绿色版本,它的用途是scons可以和自己编写的软件一起发布,这样在发布机器上就不需要额外的安装scons了。
3 源码版scons-src Packages
这个不用多说了,就是scons开发环境了。
作者一般使用scons-local。这里就用它来简单学习下。
当前最新版本是2.4.1,对应的在线文档地址:http://scons.org/documentation.html
一般分为3个文档:
man page:  http://scons.org/doc/production/HTML/scons-man.html
user doc:  http://scons.org/doc/production/HTML/scons-user.html
api doc:   http://scons.org/doc/latest/HTML/scons-api/index.html

验证本地环境:

cstriker1407@cstriker1407-x64:~/scons$ python --version  #首先看下python的版本,scons需要python2.x版本
Python 2.7.9
cstriker1407@cstriker1407-x64:~/scons$ tar -zxf scons-local-2.4.1.tar.gz #解压缩下载的压缩文件
cstriker1407@cstriker1407-x64:~/scons$ ls
sconsign.py  scons-LICENSE  scons-local-2.4.1  scons-local-2.4.1.tar.gz  scons.py  scons-README  scons-time.py
cstriker1407@cstriker1407-x64:~/scons$ python scons.py -v #最基本的命令,可以发现打印出scons的版本了。
SCons by Steven Knight et al.:
	script: v2.4.1.rel_2.4.1:3453:73fefd3ea0b0, 2015/11/09 03:25:05, by bdbaddog on ubuntu1404-32bit
	engine: v2.4.1.rel_2.4.1:3453:73fefd3ea0b0, 2015/11/09 03:25:05, by bdbaddog on ubuntu1404-32bit
	engine path: ['/home/cstriker1407/scons/scons-local-2.4.1/SCons']
Copyright (c) 2001 - 2015 The SCons Foundation
cstriker1407@cstriker1407-x64:~/scons$ 

最基本的使用方式:
首先建立两个文件:

helloworld.c

#include <stdio.h>
int main(void)
{
	printf("Hello World\n");
	return 0;
}

Sconstruct

Program('helloworld.c');

scons的使用方式其实就是读取特定的Sconstruct文件,然后根据文件来进行特定的编译。
假如上述的两个文件的位置为: 

cstriker1407@cstriker1407-x64:~/scons$ ls
examples  sconsign.py  scons-LICENSE  scons-local-2.4.1  scons-local-2.4.1.tar.gz  scons.py  scons-README  scons-time.py
cstriker1407@cstriker1407-x64:~/scons$ cd examples/helloworld/
cstriker1407@cstriker1407-x64:~/scons/examples/helloworld$ ls
helloworld.c  SConstruct

那么我们可以尝试编译一下:

cstriker1407@cstriker1407-x64:~/scons$ python scons.py -C examples/helloworld/  #可以使用C来指定工作目录,如果没有指定,默认为当前目录
scons: Entering directory `/home/cstriker1407/scons/examples/helloworld'
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o helloworld.o -c helloworld.c
gcc -o helloworld helloworld.o
scons: done building targets.  #可以看到,我们什么都没做,就在LINUX下编译了。
cstriker1407@cstriker1407-x64:~/scons$ 
cstriker1407@cstriker1407-x64:~/scons$ python scons.py -C examples/helloworld/ #如果什么都没有修改,会保持上次的编译状态
scons: Entering directory `/home/cstriker1407/scons/examples/helloworld'
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `.' is up to date.
scons: done building targets.
cstriker1407@cstriker1407-x64:~/scons/examples/helloworld$ ./helloworld  #可以执行下看看效果
Hello World
cstriker1407@cstriker1407-x64:~/scons$ python scons.py -C examples/helloworld/ -c #c是用来清理(clean)
scons: Entering directory `/home/cstriker1407/scons/examples/helloworld'
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Cleaning targets ...
Removed helloworld.o
Removed helloworld
scons: done cleaning targets.
cstriker1407@cstriker1407-x64:~/scons/examples/helloworld$ ls #可以看到清理干净了
helloworld.c  SConstruct

 

发表评论

3 − 2 =

此站点使用 Akismet 来减少垃圾评论。了解我们如何处理您的评论数据