SCons教程(3) 编译程序

本文是SCons教程的第三部分,主要讲解如何编译程序,包括重定义目标输出文件名,编译同一文件夹下的多个源文件,使用Glob创建文件列表,以及如何在共享源文件的情况下构建多个目标。通过实例演示了SCons在多文件项目中的应用和优化策略。
摘要由CSDN通过智能技术生成

编译

重定义目标输出文件名称

admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day3
$ scons
scons: Reading SConscript files ...
Finished calling Program()
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o hello.exe hello.o
gcc -o new_hello.exe hello.o
scons: done building targets.

admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day3
$ ls
SConstruct  hello.c  hello.exe  hello.o  new_hello.exe

可以看到有两个文件输出,分别是默认输出文件 hello.exenew_hello.exe

编译多文件

在实际工程中,不会只编译一个文件,这样就无法体现出来 scons 的作用了。一般情况下,是对整个工程内的多个文件共同编译,同时也可能涉及到多个文件夹。和开发者创建的工程目录有关系。

编译同一个文件夹下的多个源文件

admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day3
$ scons
scons: Reading SConscript files ...
Finished calling Program()
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o src_file1.o -c src_file1.c
gcc -o src_file2.o -c src_file2.c
gcc -o hello.exe hello.o src_file1.o src_file2.o
scons: done building targets.

admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day3
$ ls
SConstruct  hello.exe  src_file1.c  src_file2.c  ????.md
hello.c     hello.o    src_file1.o  src_file2.o

admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day3
$ hello.exe
hello
file1
file2

在生成的文件中,可以看到每个源文件都对应一个目标文件(.o),同时可以看到生成了一个 hello.exe 文件,运行该文件,可以看到分别输出三条语句。

下面我们修改一下 SConstruct 文件内容,看一下生成的文件是否有不同。

# Program(['hello.c', 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值