scons - 编译后运行程序

scons - running program after compilation

本文关键字:运行 程序 编译 scons      更新时间:2023-10-16

我想在编译后直接运行构建的程序,这样我就可以用scons构建和启动我的程序。

我认为这个SConstruct-File会在重建程序时启动程序。

main = Program( "main", [ "main.cc" ] )
test = Command( None, None, "./main >testoutput" )
Depends( test, main )

每次我运行时,这都会启动它scons

main = Program( "main", [ "main.cc" ] )
test = Command( None, None, "./main >testoutput" )
Requires( test, main )

但是两者都不起作用,我的程序从未执行过。我做错了什么?

这应该更好地工作,以便仅在构建程序时运行程序。

main = Program( "main", [ "main.cc" ] )
test = Command( target = "testoutput",
                source = "./main",
                action = "./main > $TARGET" )
Depends( test, main )

并且每次都使用 AlwaysBuild() 运行它,如@doublep所述:

main = Program( "main", [ "main.cc" ] )
test = Command( target = "testoutput",
                source = "./main",
                action = "./main > $TARGET" )
AlwaysBuild( test )

如果你想看到测试输出的内容,你可以这样做:

(假设Linux。用一些 Python 代码打印文件会更便携)

main = Program( "main", [ "main.cc" ] )
test = Command( target = "testoutput",
                source = "./main",
                action = ["./main > $TARGET",
                          "cat $TARGET"] )
AlwaysBuild( test )
每次

运行 SCons 时都会运行ls

ls = Command ('ls', None, 'ls')
AlwaysBuild ('ls')
Default ('ls')

你从来没有告诉SCons为什么以及何时应该运行你的命令。 例如,您应该将其作为依赖项添加到其他目标或使其成为默认目标。

如果你想真正始终运行命令,即无论正在构建什么目标,你可能应该使用标准的Python工具来运行它来启动外部程序。