这是要执行的代码
cp.exec("cc -Wall /tmp/test.c -o /tmp/test", function(e, stdout, stderr) {
if (e) {
var errorstr = "Compilation failed with the following error
"+ e.message.toString()
client.send(errorstr)
console.log(e, stdout, stderr)
ee.prototype.removeAllListeners()
} else if (stderr.length > 0) {
client.send("Compilion finished with warnings\n"+ stderr + '\n')
client.send('compiled')
ee.prototype.emit('compiled')
} else {
client.send("Compilation successful")
ee.prototype.emit('compiled')
}
})
'client' 是socket.io 回调参数的参数。 'ee' 是 EventEmitter 的一个实例
说到问题了。运行代码时,回调表明该命令不成功。 console.log(e, stdout, stderr) 是
{ [Error: Command failed: ] killed: false, code: false, signal: undefined } '' ''
/tmp/test.c 是一个有效的 C 代码,在检查目录 /tmp 时,我发现 test.c 是正确的,并且正在生成二进制“test”并在 shell 中运行,已正确执行。所以我不明白为什么它会标记执行失败。错误对象的信息也没有帮助。希望得到一些帮助/解释
我有点担心控制台的输出。
{ [Error: Command failed: ] killed: false, code: false, signal: undefined }
看起来不像一个正确的 JSON/JavaScript 对象,尤其是[Error: Command failed: ]
部分;至少缺少一个逗号。
建议:
从命令行运行命令并检查退出代码(使用echo $?
)。如果退出代码为 != 0,则这意味着命令“失败”(无论这意味着什么)。
当命令失败时,nodejs 说它会输入退出代码 http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback into e.code
(我在你的输出中缺少它......)。找出这个值发生了什么。
Try if(e !== null)
代替if(e)
. 不应该不过,有所作为。
不要直接调用编译器,而是调用将 stderr/stdout 重定向到文件的 shell 脚本(或使用以下命令保存副本)cc ... |& tee /tmp/cc.log
)以确保复杂设置的任何部分都不会吞没重要信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)