Scons自动大型系统构建工具的介绍与使用(原创)

Scons自动大型系统构建工具的介绍与使用
作者:余超 email:yuchao86@gmail.com


1、  概述
  scons是一个Python写的自动化构建工具,从构建这个角度说,它跟GNU make是同一类的工具。Make的关键就是构建文件之间的依赖关系,然后根据依赖关系去编译工程,而依赖关系需要人为或者说手动的去构建,对于大型系统,这个难度还是不小。Scons可以使用程序来分析文件之间的依赖关系,然后编译生成需要的文件。这个类似于automake,但是automake是用程序分析依赖,帮助生成makefile。
Scons类似于automake/autoconf,是下一代软件自动构建工具。它跨平台,能够帮助我们更方便,可靠,快速的构建软件。使用scons时,只需要编写一个SConstruct文件(它实际上是一个python文件),根据此文件,scons可以自动完成依赖关系的推导及编译链接等过程。
2、  Scons初体验
首先需要安装scons软件,直接执行命令:sudo apt-get install scons。
然后创建目录/home/yuchao/dev/scons,在该目录下分别编写helloscons.c文件和SConstruct文件,两个文件的内容分别为:
(1)编译可执行文件,直接使用Program函数即可
[yuchao@yuchao-Latitude-E5410 scons]$pwd
/home/yuchao/dev/scons
[yuchao@yuchao-Latitude-E5410 scons]$ls
helloscons.c  SConstruct
[yuchao@yuchao-Latitude-E5410 scons]$cat helloscons.c 
/**
 * This file is a like Makefile build project
 *
 * filename:helloscons.c
 * email:yuchao86@gmail.com
 *
 */


 
#include<stdio.h>
int main(int argc, char *argv[])
{
    printf("Hell Scons,I'm YuChao\n");
    return 0;
}


[yuchao@yuchao-Latitude-E5410 scons]$cat SConstruct 
# Makefile file
Program('helloscons.c');
[yuchao@yuchao-Latitude-E5410 scons]$scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o helloscons.o -c helloscons.c
gcc -o helloscons helloscons.o
scons: done building targets.
[yuchao@yuchao-Latitude-E5410 scons]$ls
helloscons  helloscons.c  helloscons.o  SConstruct
[yuchao@yuchao-Latitude-E5410 scons]$./helloscons 
Hell Scons,I'm YuChao


(2)       编译静态库
使用Library或者staticLibrary函数:
Library("helloscons",['helloscons.c'])
[yuchao@yuchao-Latitude-E5410 scons]$ls
helloscons.c  SConstruct
[yuchao@yuchao-Latitude-E5410 scons]$cat SConstruct 
# Makefile file
#Program('helloscons.c');
Library("helloscons",['helloscons.c'])
[yuchao@yuchao-Latitude-E5410 scons]$scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o helloscons.o -c helloscons.c
ar rc libhelloscons.a helloscons.o
ranlib libhelloscons.a
scons: done building targets.
[yuchao@yuchao-Latitude-E5410 scons]$ls
helloscons.c  helloscons.o  libhelloscons.a  SConstruct
[yuchao@yuchao-Latitude-E5410 scons]$


(3)编译动态库
使用SharedLibrary函数:
SharedLibrary('helloscons',['helloscons.c'])
[yuchao@yuchao-Latitude-E5410 scons]$ls
helloscons.c  SConstruct
[yuchao@yuchao-Latitude-E5410 scons]$cat SConstruct 
# Makefile file
#Program('helloscons.c');
#Library("helloscons",['helloscons.c'])
SharedLibrary('helloscons',['helloscons.c'])


[yuchao@yuchao-Latitude-E5410 scons]$scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o helloscons.os -c -fPIC helloscons.c
gcc -o libhelloscons.so -shared helloscons.os
scons: done building targets.
[yuchao@yuchao-Latitude-E5410 scons]$ls
helloscons.c  helloscons.os  libhelloscons.so  SConstruct
[yuchao@yuchao-Latitude-E5410 scons]$


4、  Scons编译多个程序
(1)以python数组的形式列出所有需要编译的源文件,如:要编译file1.c, file2.c, file3.c以生成可执行文件Hello,可这样编写SConstruct:
Program(’Hello’, [‘file1.c’, ‘file2.c’, ’file3.c’])
(2)使用正则表达式(使用Glob函数):
Program('Hello',Glob('*.c'));


[yuchao@yuchao-Latitude-E5410 scons]$ls
helloscons.c  helloSina.h  SConstruct  weibo.h
[yuchao@yuchao-Latitude-E5410 scons]$cat weibo.h 
/**
 * This file is a like Makefile build project
 *
 * filename:weibo.h
 * email:yuchao86@gmail.com
 *
 */


#ifndef _WEIBO_H_
#define _WEIBO_H_
int weibo()
{
    printf("Hell weibo,I'm YuChao\n");
    return 0;
}




#endif /* _WEIBO_H_ */ 
[yuchao@yuchao-Latitude-E5410 scons]$cat helloSina.h 
/**
 * This file is a like Makefile build project
 *
 * filename:helloSina.h
 * email:yuchao86@gmail.com
 *
 */


#ifndef _HELLOSINA_H_
#define _HELLOSINA_H_


int sina()
{
    printf("Hello SINA,I'm YuChao\n");
    return 0;
}


#endif /* _HELLOSINA_H_ */




[yuchao@yuchao-Latitude-E5410 scons]$cat helloscons.c 
/**
 * This file is a like Makefile build project
 *
 * filename:helloscons.c
 * email:yuchao86@gmail.com
 *
 */


 
#include<stdio.h>
#include"helloSina.h"
#include"weibo.h"


int main(int argc, char *argv[])
{
    printf("Hell Scons,I'm YuChao\n");
    sina();
    weibo();
    return 0;
}


[yuchao@yuchao-Latitude-E5410 scons]$cat SConstruct 
# Makefile file
Program('Hello',Glob('*.c'));
#Library("helloscons",['helloscons.c'])


[yuchao@yuchao-Latitude-E5410 scons]$scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o helloscons.o -c helloscons.c
gcc -o Hello helloscons.o
scons: done building targets.
[yuchao@yuchao-Latitude-E5410 scons]$ls
Hello  helloscons.c  helloscons.o  helloSina.h  SConstruct  weibo.h
[yuchao@yuchao-Latitude-E5410 scons]$./Hello 
Hell Scons,I'm YuChao
Hello SINA,I'm YuChao
Hell weibo,I'm YuChao
[yuchao@yuchao-Latitude-E5410 scons]$




(3)链接库
Library(‘foo’, [‘f1.c’, ‘f2.c’, ‘f3.c’])
Program(‘prog.c’, LIBS=[‘foo’, ‘bar’], LIBPATH=’.’, CCFLAGS=’DHELLO’)
5、  总结
Scons作为一个新型高效的自动软件构件工具,将大大简化我们进行软件开发和维护。
6、  参考资料
scons主页:http://www.scons.org/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值