首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

施用scons替代makefile(1)

2012-12-24 
使用scons替代makefile(1)早在多年前我刚开始接触linux下的C程序时,经常被makefile搞得焦头烂额,尤其当项

使用scons替代makefile(1)

早在多年前我刚开始接触linux下的C程序时,经常被makefile搞得焦头烂额,尤其当项目大了,子目录多了之后显得尤其不方便。后来开始学会使用开源程序的普遍做法,那就是使用automake/autoconf。时间长了仍然觉得很ugly,目录下总是一堆中间文件,显得好臃肿。去年开始我开始全面使用scons,它是一个对makefile的全面替代。

scons由python编写,受到Raymond的大力推荐,scons的官方网站上就有Raymond对scons的评价

??? “It was long past time for autotools to be replaced, and SCons has won the race to become my build system of choice. Unified builds and extensibility with Python — how can you beat that?”
??? — Eric S. Raymond, author of “The Cathedral and the Bazaar”

在Google的开源js引擎V8上也已经开始使用scons作为构建工具。

我打算分几部分对scons的日常使用做一个介绍,这篇文章作为一个入门介绍,按惯例,看一个helloworld吧。

新建一个文件,命名为hello.cpp,内容如下

#include <iostream>
using namespace std;
?
int main()
{
??? cout<<"hello,world"<<endl;
}

再创建另外一个文件,名称为SConstruct(作用类似makefile)
内容如下

Program('helloworld','helloworld.cpp')

Program是scons内建的指令,第一个参数是待生成的目标二进制文件,第二个参数是源码文件列表。

完成这个工作之后,在这个目录下执行scons,即可开始编译

leconte@localhost:~/helloword$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o helloworld.o -c helloworld.cpp
g++ -o helloworld helloworld.o
scons: done building targets.

编译完成,生成了目标文件helloworld.o和二进制文件helloworld,执行helloworld

leconte@localhost:~/helloword$ ./helloworld
hello,world

最简单的流程就是这样,很简单吧。

如果是多个文件的话,可以这样写

Program('foo', ['f1.c', 'f2.c', 'f3.c'])

另外,scons还支持通配符,例如匹配所有*.c文件

Program('program', Glob('*.c'))

scons也支持变量,一种很有用的写法是这样的

src_files = Split("""main.c
???????????????????? file1.c
???????????????????? file2.c""")
Program('program', src_files)

今天就简单介绍到这里,以后会陆续介绍更进阶的用法,同样会很简单,起码比makefile简单很多:)。

热点排行