您的位置:首页 > 其它

[SCons 有点翻译的scons学习] 1. 简单编译

2013-03-29 11:07 337 查看

安装scons

=====

这个就不详细说了,嫌麻烦可以直接sudo apt-get install scons。不嫌麻烦去下载源码安装。

简单编译

=====

来看一个最简单的hello, world程序

int

main()

{

printf("Hello, world!\n");

}

然后创建一个SConstruct文件,编写

Program('hello.c')

这个最简单的自动化编译文件有两条信息。第一指出了需要编译的结果是一个可执行文件,第二指出了输入文件是

hello.c。

这时在命令行终端运行scons,就能自动编译了。

% scons

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Building targets ...

cc -o hello.o -c hello.c

cc -o hello hello.o

scons: done building targets.

scons只需要知道输入文件的名字,就能自动推导出依赖文件,并且自动找到编译器,这里是gcc。

Building Object Files

=====

除了可以指定输出是可执行文件,还可以指定输出是.o 文件。

Object('hello.c')

输出

% scons

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Building targets ...

cc -o hello.o -c hello.c

scons: done building targets.

清理工作

=====

当编译完输出可执行文件之后,剩下的.o 文件就多余了,这时可以加个 option -c,来执行清理工作。

% scons

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Building targets ...

cc -o hello.o -c hello.c

cc -o hello hello.o

scons: done building targets.

% scons -c

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Cleaning targets ...

Removed hello.o

Removed hello

scons: done cleaning targets.

SConstruct文件

=====

和make 工具类似,Scons 是根据SConstruct 文件来组织编译的,但有一点不同,SConstruct使用的是python的语法,这点比起makefile来就爽多了。

如果觉得scons在编译的时候输出了太多信息,眼花缭乱,那么加个 -Q,这样scons: 开头的条目就被隐藏不予显示了。

C:\>scons -Q

cl /Fohello.obj /c hello.c /nologo

link /nologo /OUT:hello.exe hello.obj

embedManifestExeCheck(target, source, env)

ilding Object Files

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: