SCONS运行目标

Mur*_*dle 10 scons

我一直在寻找,看着,我无法找到我的问题的答案.我今晚刚刚开始学习scons,它看起来很棒!我虽然遇到了一点混乱.

为了便于开发,我经常喜欢让我的make文件构建我的目标,然后运行它以便我可以通过一次击键测试更改.这在make文件中非常简单:

run: $(exe)
    chmod a+x $(exe)
    $(exe)
Run Code Online (Sandbox Code Playgroud)

我已经发现我可以使用子进程这样做:

import subprocess import os.path

env = Environment();
result = env.Program(target = "FOO", source = "BAR");
location = os.path.abspath(result[0].name)
subprocess.call([location])
Run Code Online (Sandbox Code Playgroud)

但是这个解决方案存在问题.就我的实验而言,scons不会等到你的程序在启动子进程调用之前完成构建,所以你最终运行旧的可执行文件,或者如果它是干净后的构建则出错.

dar*_*rak 9

你在scons文件中做的是scons中典型的初学者错误.您假设您正在编写用于构建项目的脚本.

Scons并不像那样工作.scons文件是一个向项目添加目标的脚本.这是通过python完成的,各种对象允许您创建和操作目标,直到脚本完成.首先,项目将开始建设.

您在代码中执行的操作是描述要使用的环境,要创建的程序,然后调用运行某个程序的子进程.在此之后,项目将开始构建 - 难怪旧的可执行文件已运行,新的可执行文件尚未开始构建.

您应该做的是使用自定义构建器来执行该程序.

env = Environment() #construct your environment
files = "test.cpp" #env.Glob or list some files

#now we create some targets
program = env.Program("test",files) #create the target *program*
execution = env.Command(None,None,"./test") #create the execution target (No input & output

Depends(execution,program) #tell scons that execution depends on program
#there might be a way to get SCons to figure out this dependency itself

#now the script is completed, so the targets are built
Run Code Online (Sandbox Code Playgroud)

这里的依赖关系是明确的,程序必须在执行完成之前构建,并且它会


Pho*_*ong 6

我可能会有点迟到,但我使用Alias有这个解决方案.通过使用以下命令,它将构建并运行该程序:

$ scons run
Run Code Online (Sandbox Code Playgroud)
# Define the different target output
program = env.Program('build/output', Glob('build/test/*.cpp'))

env.Default(program)
env.Alias('run', program, program[0].abspath)
Run Code Online (Sandbox Code Playgroud)

请注意,我们使用abspath,因此它可以是跨平台的win/linux(对于linux,如果PATH未正确设置,则需要在程序名称前添加"./".