我们是否需要在每个源目录中都有 sconscript 文件 [英] Do we need sconscript file in every source directory

查看:27
本文介绍了我们是否需要在每个源目录中都有 sconscript 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 scons 来编译我的项目.在我的项目中,源文件位于不同的目录中.我们是否需要在每个目录中都有 sconscript 文件来编译那些项目源文件?

I am using scons to compile my project. In my project source files are in different directories. Do we need sconscript file in every directory to compile those project source files?

我尝试使用单个 sconscript 文件编译所有目录.但是所有目标文件都只添加到我的源目录中.

I tried to compile all directories with the single sconscript file. But all object files are adding to my source directory only.

我正在使用这个功能:

env.Library('libs',files_list)

如果 files_list 包含唯一的文件名,则 Obj 文件正在生成@variant 目录.

If files_list contains the only file names then Obj files are generating @ variant directory.

如果 files_list 包含文件路径名称,则 Obj 文件正在生成@源目录.

If files_list contains the file path names then Obj files are generating @ source directory.

你能告诉我怎么做吗?

推荐答案

我准备了一个示例,演示如何使用 SCons VariantDir() 函数仅使用一个 SConstruct 脚本(无附属 SConscripts)来编译像您这样的项目.我决定在一个单独的答案中这样做,以便更容易阅读.

I prepared an example that shows how to compile a project like yours with just one SConstruct script (no subsidiary SConscripts) using the SCons VariantDir() function. I decided to do this in a separate answer so that it would be easier to read.

VariantDir() 函数没有很好地记录,所以你提到的关于编译目标文件的放置的行为不是直接修复的.技巧"是引用变体目录中的所有源文件,而不是实际源目录中的所有源文件,如下所示.

The VariantDir() function isnt documented very well, so the behavior you mention regarding the placement of the compiled object files isnt straight-forward to fix. The "trick" is to refer to all of your source files in the variant directory, not in your actual source directory, as can be seen below.

这是我项目中源文件的结构:

Here is the structure of the source files in my project:

$ tree .
.
├── SConstruct
├── src1
│   ├── class1.cc
│   └── class1.h
├── src2
│   ├── class2.cc
│   └── class2.h
└── srcMain
    └── main.cc

这是 SConstruct:

Here is the SConstruct:

env = Environment()

# Set the include paths
env.Append(CPPPATH = ['src1', 'src2'])

# Notice the source files are referred to in the build dir
# If you dont do this, the compiled objects will be in the src dirs
src1Sources = ['build/lib1/class1.cc']
src2Sources = ['build/lib2/class2.cc']
mainSources = ['build/mainApp/main.cc']

env.VariantDir(variant_dir = 'build/lib1', src_dir = 'src1', duplicate = 0)
env.VariantDir(variant_dir = 'build/lib2', src_dir = 'src2', duplicate = 0)
env.VariantDir(variant_dir = 'build/mainApp', src_dir = 'srcMain', duplicate = 0)

lib1 = env.Library(target = 'build/lib1/src1', source = src1Sources)
lib2 = env.Library(target = 'build/lib1/src2', source = src2Sources)
env.Program(target = 'build/mainApp/main', source = [mainSources, lib1, lib2])

这是编译输出:

$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o build/lib1/class1.o -c -Isrc1 -Isrc2 src1/class1.cc
ar rc build/lib1/libsrc1.a build/lib1/class1.o
ranlib build/lib1/libsrc1.a
g++ -o build/lib2/class2.o -c -Isrc1 -Isrc2 src2/class2.cc
ar rc build/lib1/libsrc2.a build/lib2/class2.o
ranlib build/lib1/libsrc2.a
g++ -o build/mainApp/main.o -c -Isrc1 -Isrc2 srcMain/main.cc
g++ -o build/mainApp/main build/mainApp/main.o build/lib1/libsrc1.a build/lib1/libsrc2.a
scons: done building targets.

这是编译后得到的项目结构:

And here is the resulting project structure after compiling:

$ tree .
.
├── build
│   ├── lib1
│   │   ├── class1.o
│   │   ├── libsrc1.a
│   │   └── libsrc2.a
│   ├── lib2
│   │   └── class2.o
│   └── mainApp
│       ├── main
│       └── main.o
├── SConstruct
├── src1
│   ├── class1.cc
│   └── class1.h
├── src2
│   ├── class2.cc
│   └── class2.h
└── srcMain
    └── main.cc

应该提到的是,更直接的方法是使用 SConscript() 函数,指定 variant_dir,但如果您的要求不允许您这样做,则此示例将起作用.SCons 手册页 提供了有关 VariantDir() 函数的更多信息.您还会在那里找到以下内容:

It should be mentioned that a more straight-forward way to do this is with the SConscript() function, specifying the variant_dir, but if your requirements dont allow you to do so, this example will work. The SCons man page has more info about the VariantDir() function. There you will also find the following:

请注意,VariantDir() 在附属 SConscript 文件中最自然地工作.

Note that VariantDir() works most naturally with a subsidiary SConscript file.

这篇关于我们是否需要在每个源目录中都有 sconscript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆