Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3045700
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2017-04-19 14:03:41

早在多年前我刚开始接触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
using namespace std;
 
int main()
{
    cout<<"hello,world"<
}

再创建另外一个文件,名称为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简单很多:)。
阅读(1380) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~