scons的使用方法和进阶

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\(\
  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于使用QNX操作系统的Qt项目,可以使用SCons作为构建工具。SCons是一种基于Python的构建工具,可以自动化构建和测试过程,提高项目开发效率。 以下是使用SCons编译Qt项目的基本步骤: 1. 安装SCons 在QNX操作系统上,可以使用pkgsrc安装SCons。在终端中输入以下命令: ``` pkgin update pkgin install scons ``` 2. 配置SCons 在Qt项目的根目录下创建一个SConstruct文件,并添加以下内容: ``` import os # Set up environment variables env = Environment() env.Append(CPPPATH=['/usr/include/qt']) # Set up Qt libraries and modules env.Append(LIBS=['QtCore', 'QtGui', 'QtNetwork', 'QtXml', 'QtSql', 'QtSvg']) env.Append(QTDIR='/usr/local/Trolltech/Qt-4.8.7') # Set up compiler and linker flags env.Append(CCFLAGS=['-Wall', '-Werror']) env.Append(LINKFLAGS=['-Wl,-rpath-link,/usr/local/Trolltech/Qt-4.8.7/lib']) # Compile and link the project env.Program(target='myproject', source=['main.cpp', 'myclass.cpp']) ``` 在上面的代码中,我们设置了环境变量,包括Qt的头文件路径、库文件路径和Qt安装路径。然后,我们添加了需要链接的Qt库,设置编译器和链接器的参数,最后编译并链接项目。 3. 运行SCons 在终端中进入项目根目录,运行以下命令: ``` scons ``` 这将会自动编译和链接项目,并生成可执行文件。 注意:在运行SCons之前,需要确保Qt已经正确安装并配置好。如果Qt库文件没有正确设置,可能会出现链接错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值