121 项目 013 番外向 scons

本文介绍如何使用SCons构建工具进行C语言程序的编译和链接,包括安装SCons、基本用法、多文件编译、库文件的编译与链接等,并提供了实际操作示例。

安装

yum install scons

简单使用

源文件

#include<stdio.h>
int main(void){
    printf("\nHello Linux Programing ! By LaoLang!\n\n");
    return 0;
}

SConstruct

Program('hello.c')

使用

[root@laolang hello]# scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o hello hello.o
scons: done building targets.
[root@laolang hello]# ./hello

Hello Linux Programing ! By LaoLang!

[root@laolang hello]# 

命令行选项

  • -c 即是clean
[root@laolang hello]# scons -c
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Cleaning targets ...
Removed hello.o
Removed hello
scons: done cleaning targets.
[root@laolang hello]# 
  • -Q 是输出尽可能少的信息
[root@laolang hello]# scons -Q
gcc -o hello.o -c hello.c
gcc -o hello hello.o
[root@laolang hello]# 

编译相关

一个多文件例子

源码

  • hello.c
#include<stdio.h>
#include"sum.h"
int main(void){
    int a = 0;
    int b = 0;
    int sums = 0;
    printf("请输入两个整数:");
    scanf("%d %d",&a,&b);
    sums = add(a,b);
    printf("%d + %d = %d\n",a,b,sums);

    printf("\nHello Linux Programing ! By LaoLang!\n\n");
    return 0;
}
  • sum.c
#include"sum.h"


int add( int a, int b ){
    return a + b;
}

int minus( int a , int b ){
    return a - b;
}

int mult ( int a, int b ){
    return a - b;
}

int div( int a, int b ){
    if( 0 != b ){
        return a / b;
    }else{
        return 0;
    }
}
  • sum.h
#ifndef SUM_H
#define SUM_H

int add( int a, int b );

int minus( int a, int b );

int mult( int a, int b );

int div( int a , int b );

#endif

SConstruct

src_files=Split('hello.c sum.c')
Program(source=src_files,target='cal')

具体操作过程和第一个例子没有什么不同

指定源文件的方式

  • 直接在 Program 函数中写
Program('cal',['hello.c','sum.c'])
  • 使用Glob结合正则
Program('cal',Glob('*.c'))
  • 使用 Split
src_files=Split("""
hello.c
sum.c
""")
Program('cal',src_files)
  • 使用关键字

source 指定源文件列表

target 指定目标程序名称

在上面的例子中就是用的这种方式

编译多个目标程序

多次调用Program函数即可

编译和连接库

文件布局

[root@laolang libs]# tree
.
├── cal
│   ├── cal.c
│   └── cal.h
├── main.c
└── SConstruct

1 directory, 4 files
[root@laolang libs]# 

SConstruct

cal_src_files=Split("""
./cal/cal.c
""")
Library('cal',cal_src_files)

Program('mycal','main.c',LIBS=['cal'],LIBPATH='.')

使用

[root@laolang libs]# scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o cal/cal.o -c cal/cal.c
ar rc libcal.a cal/cal.o
ranlib libcal.a
gcc -o main.o -c main.c
gcc -o mycal main.o -L. -lcal
scons: done building targets.
[root@laolang libs]# l
total 28K
drwxr-xr-x. 2 root root 4.0K Sep 30 09:26 cal/
-rw-r--r--. 1 root root 1.8K Sep 30 09:26 libcal.a
-rw-r--r--. 1 root root  305 Sep 30 08:57 main.c
-rw-r--r--. 1 root root 2.0K Sep 30 09:26 main.o
-rwxr-xr-x. 1 root root 7.2K Sep 30 09:26 mycal*
-rw-r--r--. 1 root root  122 Sep 30 09:24 SConstruct
[root@laolang libs]# tree
.
├── cal
│   ├── cal.c
│   ├── cal.h
│   └── cal.o
├── libcal.a
├── main.c
├── main.o
├── mycal
└── SConstruct

1 directory, 8 files
[root@laolang libs]# ./mycal 
请输入两个整数:4 5
4 + 5 = 9

Hello Linux Programing ! By LaoLang!

[root@laolang libs]# 

后记

也就到此为止了,因为动态连接库我没有测试成功,而且没有发现怎么把构建产生的中间文件放到源码之外的目录,也就是我没发现Scons 对 build int output 的支持,虽然在clean的时候会保持源码目录的干净,不过我还是打算不用了,也就写一引起小程序还可能会用。

转载于:https://my.oschina.net/iamhere/blog/753570

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值