scons编译简介以及scons: *** No SConstruct file found. 问题解决方案

Scons是什么
1. make
  在Linux系统上做过c/c++开发的同学们都知道一个项目工程管理工具:make和Makefile。

  make 这个工具自上个世纪 70 年代 Stuart Feldman 在贝尔实验室开发出以来,就一直是类 UNIX 程序员的最爱之一。

  通过检查文件的修改时间,make 工具可以知道编译目标文件所要依赖的其他文件。在复杂的项目中,如果只有少数几个文件修改过,make 工具知道仅仅需要对哪些文件重新编译就可以确保目标程序被正确的编译链接。这样做的好处就是在编译中,不仅可以节省大量的重复输入,还可以确保程序可以被正确的链接,缩短编译的时间。

  虽然如此,但是为 make 工具编写建造规则却不是一件容易的事。它复杂的配置规则,即使是有经验的开发者也望而生畏。

  那么,今天介绍一个它的同类产品,也可以说是升级产品:Scons,它做的事情跟make一样,但更简单,更容易。

2.Scons
  Scons是一个开放源码、以Python语言编码的自动化构建工具,可用来替代make编写复杂的makefile。并且scons是跨平台的,只要scons脚本写的好,可以在Linux和Windows下随意编译。

  SCons 的设计目标就是让开发人员更容易、更可靠和更快速的建造软件。

  与传统的 make 工具比较,SCons 具有以下优点:

  • 使用 Python 脚本做为配置文件。
  • 对于 C,C++ 和 Fortran, 内建支持可靠自动依赖分析 . 不用像 make 工具那样需要 执行"make depends"和"make clean"就可以获得所有的依赖关系。
  • 内建支持 C, C++, D, Java, Fortran, Yacc, Lex, Qt,SWIG 以及 Tex/Latex。 用户还可以根据自己的需要进行扩展以获得对需要编程语言的支持。
  • 支持 make -j 风格的并行建造。相比 make -j, SCons 可以同时运行 N 个工作,而 不用担心代码的层次结构。
  • 使用 Autoconf 风格查找头文件,函数库,函数和类型定义。
  • 良好的夸平台性。SCons 可以运行在 Linux, AIX, BSD, HP/UX, IRIX, Solaris, Windows, Mac OS X 和 OS/2 上。

3.Scons使用
3.1 安装
  在 SCons 的官方网站上可以查每个平台的具体安装方法。

  Win平台的下载和安装就不说了。

  Linux下下载tar包,执行以下命令即可:

tar -zxf scons-2.0.1.tar.gz
cd scons-2.0.1
sudo python setup.py install


  对于 Linux 来说,scons 会默认安装到 /usr/loca/bin 目录下,而在 Windows 平台上,则会被安装到 C:Python25Scripts 下。

3.2 使用
  这里通过一个简单的示例来说明。

  一个hello.c源文件,一个SConstruct文件,后面再解释。

% ls
hello.c SConstruct

% cat hello.c
#include <stdio.h>

int main(void)
{
printf("hello, world!
");

return 0;
}

  运行Scons,得到如下:

% 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.

% ls
hello hello.c hello.o SConstruct

% ./hello
hello, world!

  就是这样,程序编译完成了。到这里Scons似乎与make没有什么两样,同样的操作,只是把scons换成make,SConstruct换成Makefile。

  对,不同的地方就在于,下面的展示:

% cat SConstruct
Program("hello.c")

  就这一行,编译就完成了。

  事实确实如此,它比传统的 Makefile 简单很多。SConstruct 以 Python 脚本的语法编写,你可以像编写 Python 脚本一样来编写它。其中的 Program 是编译的类型,说明你准备想要建造一个可执行的二进制程序,它由 hello.c 文件来生成。在这里,没有指定生成的可执行程序的名字,SCons 会把源代码文件名字的后缀去掉,用来作为可执行文件的名字。

  我们甚至不需要像 Makefile 那样指定清理的动作,就可以执行清理任务。在 SCons 中,执行清理任务由参数 -c 指定,如下 :

% scons -c
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Cleaning targets ...
Removed hello.o
Removed hello
scons: done cleaning targets.

% ls
hello.c SConstruct

关于scons: *** No SConstruct file found.的解决方案

   使用SCons时有3种方式指定SConstruct文件,如下:

  • scons从项目的根目录执行,这里应该有一个 SConstruct 文件。这是最标准的方式。

  • 从项目的子目录中,在根目录下应该有一个 SConsctruct 文件,scons使用以下选项之一(如 scons -h 所见)执行以告诉它查找 SConstruct 的目录结构

-u, --up, --search-up
Search up directory tree for SConstruct, build targets at or 
below current directory.

-U
Search up directory tree for SConstruct, build Default() targets 
from local SConscript.
  • 显式指定 SConstruct 文件的位置,这也可以从 scons -h
-f FILE, --file=FILE, --makefile=FILE, --sconstruct=FILE
Read FILE as the top-level SConstruct file.

  这是目录中的示例项目,其/home/notroot/projectDir目录结构如下:

SConstruct
subdir/file.hh
subdir/file.cc

以下是如何使用上述不同选项:

  选项1:  

  从项目根目录执行 scons

# cd /home/notroot/projectDir
# scons  

  选项 2:

  从项目目录中执行 scons 并告诉它查找 SConstruct 的目录层次结构

# cd /home/notroot/projectDir/subdir
# scons -u

  选项 3:

  从项目目录中执行 scons 并指定 SConstruct 的路径

# cd /home/notroot/projectDir/subdir
# scons -f /home/notroot/projectDir/SConstruct
原文地址:https://www.cnblogs.com/z1141000271/p/14963548.html