简体   繁体   English

SCons out of source build仅适用于可执行程序,而不适用于目标文件

[英]SCons out of source build only works for executable program, not for object files

I have the following SCons configuration: 我有以下SCons配置:

current_directory
|-<.cpp files>
|-<.h files>
|-SConstruct
|-SConscript
|-bin
  |-<empty>

I want to build my source files and put the executable and the object files into the bin directory. 我想构建我的源文件,并将可执行文件和目标文件放入bin目录。

This is what I have in my SConstruct file: 这是我在SConstruct文件中的内容:

SConscript('SConscript', variant_dir='bin', duplicate=0)

While in the SConsript file I have: SConsript文件中我有:

debug_environment.Program(target = 'SsaTest', src_files, LIBS=libraries, LIBPATH=libraries_path)

When I build using scons command I get the SsaTest executable in the bin directory (as desired), but the object files are left in the current directory. 当我使用scons命令构建时,我在bin目录中获取SsaTest可执行文件(根据需要),但目标文件保留在当前目录中。

How can I have the .o files be built in the bin directory as well? 我怎样才能在bin目录中构建.o文件?

Many thanks. 非常感谢。

EDIT: Complete SConscript file (forgive me for the xxx s) 编辑:完整的SConscript文件 (原谅我的xxx

import os

# This is correctly Exported()
Import('debug_flags')

# Paths to header files
headers_paths = ['#/../../xxx/include/',
                 '#/../../../xxx/include/',
                 '#/../../xxx/include/',
                 '#/../../xxx/include/',
                 '#/../../xxx/include/']

# Path to source files
src_folder = '#./'

# Source files list
src_files = ['xxx.cpp',
             'xxx.cpp',
             'xxx.cpp',
             'xxx.cpp',
             'xxx.cpp']

# Prepend the relative path to each source file name
src_files = [src_folder + filename for filename in src_files]

libraries = ['xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx']

libraries_path = ['#/../../xxx/lib',
                  '#/../../../xxx/bin', 
                  '#/../lib', 
                  '#/../../xxx/lib', 
                  '#/../../xxx/lib', 
                  '#/../../xxx/lib']

# Debug environment
debug_environment = Environment(CC = 'g++', CCFLAGS=debug_flags, ENV = os.environ, CPPPATH=headers_paths); 
# Executable build command
debug_environment.Program(target = 'SsaTest', src_files, LIBS=libraries, LIBPATH=libraries_path)

Using '#' with source files not recommended, because you have your situation, scons can't correctly process it with variant dirs and how result create object files in directory where sources placed. 使用'#'与不推荐的源文件,因为你有你的情况,scons无法使用变量dirs正确处理它以及结果如何在放置源的目录中创建目标文件。

So, i tryed to build your example with same configuration and have no troubles: 所以,我尝试用相同的配置构建你的例子并且没有麻烦:

#SConsctruct
SConscript('SConscript', variant_dir='bin', duplicate=0)

#SConscript
src_files = Glob('*.cpp')
debug_environment = Environment()
debug_environment.Program('SsaTest', src_files)

So, all object files are generated in bin directory. 因此,所有目标文件都在bin目录中生成。

Finally, you have no troubles with relation dirs with sources files then using variant dirs. 最后,您对源文件的关系目录没有麻烦,然后使用变量目录。 But include dirs are depended from variant dirs. 但是包括dirs取决于变体dirs。 Configuration for example : 配置例如:

build
app
--SConscript
--src
----*.h
----*.cpp
SConstruct

#SConstruct
rootEnv = Environment()
Export('rootEnv')
SConscript('app/SConscript', variant_dir='build', duplicate=0)

You SConscript will be looking like it: 你的SConscript看起来会像这样:

Import('rootEnv')
env = rootEnv.Clone()
env.Append(CPPPATH = ['#app/src'])
env.Program('app', Glob('src/*.cpp'))

'#app/src' - where # is very important when using variant dir, because if would be app/src, build command will be looking: '-Ibuild/app/src' (adding variant dir before include path). '#app / src' - 其中#在使用variant dir时非常重要,因为如果是app / src,build命令将会显示:' - Ibuild / app / src'(在包含路径之前添加变量dir)。 But adding '#' command will be looking correctly : '-Iapp/src'. 但添加'#'命令将正确显示:' - Iapp / src'。

One thing that sticks out in your SConscript is how you are prepending the path to each source file with #./ . 在您的SConscript中突出的一件事是您如何使用#./前置每个源文件的路径。

# Path to source files
src_folder = '#./'

Why do you use the dot in that path, its not necessary? 你为什么在那条路上使用圆点,这不是必要的? Try with the following #/ like you do with the rest of the paths, like this: 尝试使用以下#/就像使用其他路径一样,如下所示:

# Path to source files
src_folder = '#/'

Another option would be to put the source files and the respective SConscript in its own subdirectory. 另一种选择是将源文件和相应的SConscript放在它自己的子目录中。 Its not real clear why you have a SConstruct, SConscript, and the source files all in one directory. 它不清楚为什么你有一个SConstruct,SConscript和源文件都在一个目录中。 Either create a subdir, or consider removing the SConscript if its not necessary. 创建一个子目录,或者考虑删除SConscript(如果没有必要)。

In the SConscript() function call in the SConstruct, refer to the variant_dir as "#bin" and not "bin". 在SConstruct中的SConscript()函数调用中,将variant_dir称为“#bin”而不是“bin”。 Not sure if this will help, but its better practice. 不确定这是否有帮助,但更好的做法。

Ive seen this behaviour before using the Repository() SCons function to reference the source files as mentioned here . 在使用Repository()SCons函数引用此处提到的源文件之前,我已经看到了这种行为。

Also, this is off-topic, but if your include and library paths (headers_paths and libraries_path variables) are outside of the project directory structure, you may consider using absolute paths instead. 此外,这是偏离主题的,但如果您的包含和库路径(headers_paths和libraries_path变量)不在项目目录结构中,您可以考虑使用绝对路径。 Personally I find it rather ugly to use relative paths with several ../ paths. 就个人而言,我发现使用具有多个../路径的相对路径相当丑陋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM