scons的使用方法和进阶

本文介绍了scons与make的区别,详细讲解了scons的安装、基本使用,包括编译单个文件、清理项目、指定编译目标等操作。还深入探讨了scons的进阶技巧,如防止环境变量冲突、导入外部环境变量和参数文件的应用。通过实例展示了scons在编译过程中的自动化依赖关系推导和高效管理。
摘要由CSDN通过智能技术生成

scons 简要说明

写在前面

scons和make有什么不同

make中使用了目标和为伪目标相互依赖的方法将需要编译的项目和
依赖的目标链接起来。比如一个.bin文件依赖.o ,而.o文件依赖于
.s 或者.c。

对于编译项目,需要处理的基本问题就是如上的需求。
而scons的编译函数中都有一个编译目标 和编译来源的入参,因此scons
在编译过程中会自动推导依赖关系,而不用显示的去指明依赖链条.

scons 安装

pip install scons

scons 的基本使用

1. 编译单个文件和常用的操作介绍

  1. 如何编译一个单文件
    1.1 比如现在需要编译一个main.cpp,在main.cpp所在目录执行下边命令
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ echo Program\(\"main.cpp\"\) > Sconstruct
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ cat Sconstruct 
    Program("main.cpp")
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ scons
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    g++ -o main.o -c main.cpp
    g++ -o main main.o
    scons: done building targets.
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ ls
    main  main.cpp  main.o  Sconstruct
    
    1.2 分析上边的过程Program函数是用来生成最终的执行目标,可以自动推导默认的编译方法编译出最终目标
  2. 清理项目
    2.1 清理命令是 scons -c
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ ls
    main  main.cpp  main.o  Sconstruct
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ 
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ scons -c
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Cleaning targets ...
    Removed main.o
    Removed main
    scons: done cleaning targets.
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ ls
    main.cpp  Sconstruct
    
    2.2 scons 上不用去专门编写项目的清理方法,scons会自动由最终结果往前的这个链条上所有中间文件,并删除掉
  3. 打印依赖树
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ scons --tree=all
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    g++ -o main.o -c main.cpp
    g++ -o main main.o
    +-.
      +-Sconstruct
      +-main
      | +-main.o
      | | +-main.cpp
      | | +-/usr/bin/g++
      | +-/usr/bin/g++
      +-main.cpp
      +-main.o
        +-main.cpp
        +-/usr/bin/g++
    scons: done building targets.
    
  4. 静默编译
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ scons -Q
    g++ -o main.o -c main.cpp
    g++ -o main main.o
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ scons -c -Q
    Removed main.o
    Removed main
    
  5. 为输出目标命名
    ggg@ggg-X550JX ~/test_demo/test_demo/pro1 $ echo Program\(\
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值