为什么Scons找不到我项目的源文件?

3

这是我在Github上的项目:链接

这是我的SConstruct文件:

SConscript('main.scons', variant_dir = 'build', duplicate = 0)

这是我的main.scons文件:
import sys
import os
import fnmatch

def find_source_files(directory, ext = "cpp"):
    matches = []
    for root, dirnames, filenames in os.walk(directory):
      for filename in fnmatch.filter(filenames, '*.' + ext):
          matches.append(os.path.join(root, filename))
    return matches

if __name__ == '__main__':
    for f in find_source_files('src'):
        print f
else: 
    Program(target = 'main.bin', source = find_source_files('src'))

这是我运行它时获得的结果:
bitcycle @ cypher ~/git/IeiuniumTela $ find $(pwd) -name "*.bin" -or -name "*.o" -exec rm {} \;;  scons; find $(pwd) -name "*.bin" -or -name "*.o"

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build
gcc -o build/main.bin
gcc: fatal error: no input files
compilation terminated.
scons: *** [build/main.bin] Error 4
scons: building terminated because of errors.

当我运行`python main.scons'来测试它时,以下是发生的事情:

bitcycle @ cypher ~/git/IeiuniumTela $ python main.scons
src/main.cpp

我很难理解为什么它找不到我的源文件。有什么建议或想法吗?

[更新] 从邮件列表中得到了一些好的指导后,我发现这对我来说“足够好了”。

/SConstruct: SConscript('src/main.scons', variant_dir = 'build', duplicate = 0)

/src/main.scons: Program(target = 'main.bin', source = Glob('*.cpp'))

请参见github存储库以获取完整的源树。为了完整起见,我还添加了一个空的构建目录到存储库中。我发现有趣的是:

a. 在这个构建工具的上下文中,SCons版本的Glob不是递归的,用于发现源代码。我希望有一个递归的发现选项。:(

b. 我需要将scons文件放在与源文件相同的目录中(这很烦人)。

c. 打印语句显然有效,但sys.stdout.write不行(来自python模块)。


我在Scons用户邮件列表中提出了一个问题,我们将看看会有什么结果。 - bitcycle
2个回答

0

可能是因为您的 main.scons 文件已经在 src 目录中,而您的 find_source_file 实际上正在搜索 src/src?当我将 scons 文件移动到顶级目录时,它为我找到了源代码。

更新: 经过调查,variant_dir 将工作目录设置为 build,因此您的 find_source_files 将在 build/src 中寻找文件并找不到任何内容。最好从 SConstruct 文件中调用 find_source_files,或在 main.scons 中使用 VariantDir()


如果 find_source_files 函数失败了,那么直接执行它(通过 python main.scons 命令)将不会打印出源文件列表。:( - bitcycle
抱歉,实际上对我也没有起作用。我已经更新了答案。 - none_00

0

SCons在处理相对目录路径方面与Python不同,因此我不会惊讶地看到测试执行和SCons执行之间的差异。通常,在SCons中,所有内容都是相对于根SConstruct脚本或SConscript脚本的。

您的代码似乎正确,但是考虑在find_source_files()中添加一些调试打印语句,以了解发生了什么。

也许您计划稍后更广泛地使用find_source_files()函数,但对于一个源文件的简单情况而言,似乎您正在使事情变得过于复杂,并且可以在main.scons中直接使用以下内容:

Program(target = 'main.bin', source = 'src/main.cpp')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接