使用scons替代makefile(2)

本文介绍使用SCons编译静态库和动态库的方法。通过具体示例展示了如何编写SConstruct文件来编译静态库libdemo.a及动态库libdemo.so,并介绍了如何在程序中使用这些库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇文章接着上一篇进一步介绍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文件

 

源文档 <http://hi.baidu.com/jrckkyy/blog/item/73645b50e27b514c1138c222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值