2

我有以下 C++ 代码

...
int res = system("python myscript.py");
if(res != 0){
    cerr << "Exit code was:" << res << endl;
}

python脚本文件以

print "This will exit with code 0"
sys.exit(0)

如果我直接运行 python 脚本,我会得到正确的退出代码 (0)。但是,如果我通过 c++ 应用程序运行它,res 为 -1,即使正确打印了“This will exit with code 0”行。

奇怪的是,如果我将调用移到 C++ 应用程序执行的开头,系统调用返回的退出代码是正确的。

什么会导致它在此过程中变得“错误”?

编辑:

在围绕该问题添加一些调试“cout”信息后......它消失了。看起来我有一个Heisenbug。

4

1 回答 1

2

你确定脚本正在运行吗?

尝试

...
int res = system("/usr/bin/python /path/to/myscript.py");
if(res != 0){
    cerr << "Exit code was:" << res << endl;
}
于 2013-01-15T18:25:10.670 回答