`

使用scons替代makefile(2)

阅读更多

本篇文章接着上一篇进一步介绍scons的使用方法,主要介绍静态库和动态库的编译

在scons里编译库文件和编译可执行程序非常类似,只是采用的指令有所不同。
对于静态库,采用如下指令

    Library(”libdemo”,["a.c","b.c"])或者StaticLibrary(”libdemo”,["a.c","b.c"])

对于动态库,采用如下指令

    SharedLibrary(”libdemo”,["a.c","b.c"])

下面来看一个最简单的例子吧,假如有两个源文件a.c和b.c,内容分别如下
a.c中定义了函数demo_a

[leconte@localhost demolib]$ cat a.c
#include <stdio.h>
void demo_a(char* s)
{
    printf("%s\n",s);
}

b.c中定义了函数demo_b

[leconte@localhost demolib]$ cat b.c
#include <stdio.h>
void demo_b()
{
    printf("hello world\n");
}

在同一个目录下编写Sconstruct文件,编译静态库libdemo.a

[leconte@localhost demolib]$ cat SConstruct
Library("libdemo",["a.c","b.c"])

然后进行编译

[leconte@localhost demolib]$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o a.o -c a.c
gcc -o b.o -c b.c
ar rc libdemo.a a.o b.o
ranlib libdemo.a
scons: done building targets.

编译过程中会生成各自的目标文件a.o和b.o,然后直接打包为静态库libdemo.a。

接下来仍然用a.c和b.c来编译动态库libdemo.so
修改Sconstruct内容为

[leconte@localhost demolib]$ cat SConstruct
SharedLibrary("libdemo",["a.c","b.c"])

执行scons,过程如下

[leconte@localhost demolib]$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o a.os -c -fPIC a.c
gcc -o b.os -c -fPIC b.c
gcc -o libdemo.so -shared a.os b.os
scons: done building targets.

它会首先生成位置无关的目标文件a.os和b.os,然后再生成动态库libdemo.so

[leconte@localhost demolib]$ ldd libdemo.so
        linux-gate.so.1 =>  (0x0019a000)
        libc.so.6 => /lib/libc.so.6 (0x00e04000)
        /lib/ld-linux.so.2 (0x00252000)

OK,下面的问题就是如何使用动态库或者静态库了,编写一个简单的程序main.c,内容如下

#include <stdio.h>
void demo_a(char* s);
void demo_b();
 
int main()
{
    demo_b();
    demo_a("www.linuxers.cn");
}

在之前的Sconstruct中加入一行,生成demo程序,它依赖libdemo.a静态库

Library("libdemo",["a.c","b.c"])
Program("demo","main.c",LIBS=["demo"],LIBPATH=["."]);

在Sconstruct中需要用LIBS指令指定需要链接的库,用LIBPATH指定库位置
编译并执行过程如下

[leconte@localhost demolib]$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o a.o -c a.c
gcc -o b.o -c b.c
gcc -o main.o -c main.c
ar rc libdemo.a a.o b.o
ranlib libdemo.a
gcc -o demo main.o -L. -ldemo
scons: done building targets.
[leconte@localhost demolib]$ ./demo
hello world
www.linuxers.cn

scons最基本的编译库和使用库的方式就是这样,很容易将这种方式扩展到你的项目中。
当然,实际项目中不可缺少的一点是多目录编译,这一点上scons类似于makefile,都有包含机制。
具体例子如下

[leconte@localhost demolib]$ cat SConstruct
SConscript(['test1/SConscript',
    'test2/SConscript',
    'test3/SConscript',
    'test4/SConscript'])

编译过程中,会自动编译 test1~test4目录下的源文件,当然具体的编译过程依赖于各自目录下的Sconstruct文件

分享到:
评论

相关推荐

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

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

    scons用户手册(英文)

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

    scons2-2-0

    scons-2.2.0.rar使用工具

    scons3.0.1 scons-design

    scons3.0.1 scons-design

    Scons使用手册

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

    scons控制编译工程例子

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

    scons-2.5.1-2.all.zip

    scons-2.5.1-2.all.zip

    scons3.0.1 scons-man

    scons-manscons-man

    Python2.6 scons1.3

    Python2.6 scons1.3

    python和scons安装

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

    scons用户手册

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

    scons 用户使用手册

    scons 用户使用手册

    scons-2.5.1.tar.gz

    scons

    scons使用设计手册

    scons使用设计手册

    scons-2.4.0.zip

    scons是一个Python写的自动化构建工具,同Linux的make工具功能相似。

    软件构建工具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-2.3.6 安装文件

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

Global site tag (gtag.js) - Google Analytics