`

使用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简单很多:)。

分享到:
评论

相关推荐

    发掘Scons——替代Make的Python工具

    发掘Scons——替代Make的Python工具:Scons是用Python实现的一款跨平台的开源Build Tool,用Python实现意味着Scons比Make所使用的类Shell语言更贴近于自然语言,更易于理解和控制;用Python实现的另一个好处也是Make...

    scons用户手册(英文)

    scons类似于makefile,只是scons是python编写的,并且搭建scons比搭建makefile简单,其中python语言也很容易学习,不过这个文档没有详细的一个大型项目的搭建用例,请注意是英文版

    scons控制编译工程例子

    linux一般通过makefile控制编译。linux代码目录结构是 +scons_project_root 【src】 #源代码目录 [dir1] [dir2] [dir3] main.c 【target】 #目标输出目录 [include] [lib] [bin] 通过scons也实现...

    scons3.0.1 scons-design

    scons3.0.1 scons-design

    Scons使用手册

    SCons脚本编辑器,和Makefile相似。但是,新手学习比较快,简单易行!

    scons3.0.1 scons-man

    scons-manscons-man

    python和scons安装

    python和scons安装包及安装截图说明,适合windows 64位系统。没找到分值设置位置,建议版主设置分数为1分

    scons用户手册

    scons用户手册,scons是一个编译工具,类似于Makefile,但是相对于Makefile ,它语法类似于Python,很方便。这个是官方的资料。

    scons 用户使用手册

    scons 用户使用手册

    scons2-2-0

    scons-2.2.0.rar使用工具

    scons-2.5.1.tar.gz

    scons

    scons使用设计手册

    scons使用设计手册

    Python2.6 scons1.3

    Python2.6 scons1.3

    软件构建工具scons3源码

    scons有非常多相对于make构建系统的优秀特性,可是因为发展时间比較短如今的应用范围还是不太多,可以找到的资料也不是非常多。 scons如今一大问题就是初始上手还是有点难度的,对于有python的基础的还是有点问题,...

    自动化编译工具scons简介

    自动化编译工具scons简介 自动化编译工具scons简介 自动化编译工具scons简介

    scons-3.0.5.zip

    scons-3.0.5.zip windows平台下的scons。1.本人用来mong编程生成库所使用的。2.其他功能不清楚3.搬运工免费

    scons-local-2.0.1

    scons是一个Python写的自动化构建工具,从构建这个角度说,它跟GNU make是同一类的...它是一种改进,并跨平台的gnu make替代工具,其集成功能类似于autoconf/automake 。scons是一个更简便,更可靠,更高效的编译软件。

    SCons用户指南.pdf

    SCons用户指南.pdf

    scons-2.2.0.tar.gz Ubuntu安装包

    scons-2.2.0.tar.gz Ubuntu安装包

Global site tag (gtag.js) - Google Analytics