英文:
golang exec.Command couldn't run C binaries
问题
我正在使用exec.Command和exec.Start()执行一个C可执行文件。我的C可执行文件位于当前目录,以下是我的代码:
cmd := exec.Command("./a.out")
// stdout,err := cmd.StderrPipe()
stderr,_ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
log.Printf("Waiting for command to finish...")
s,err := ioutil.ReadAll(stderr)
log.Printf(string(s))
err = cmd.Wait()
log.Printf("Command finished with error: %v", err)
当我运行这段代码时,我得到的输出是:
2014/12/10 07:49:39 Waiting for command to finish...
2014/12/10 07:49:39 Command finished with error: exit status 11
我的C hello world程序如下:
#include <stdio.h>
int main(){
printf("hello world");
}
C代码是使用gcc版本4.8.2编译的,可执行文件名为a.out。
C可执行文件a.out是一个简单的hello world程序,我得到了正确的输出,但我不明白为什么exec.Wait()以状态码11而不是0退出。
当我在Linux终端上运行时,可执行文件('a.out')正常运行,没有任何故障或错误。
英文:
I executing a C executable using exec.Command and exec.Start(). My C executable is in the current directory and this is my code
cmd := exec.Command("./a.out")
// stdout,err := cmd.StderrPipe()
stderr,_ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
log.Printf("Waiting for command to finish...")
s,err := ioutil.ReadAll(stderr)
log.Printf(string(s))
err = cmd.Wait()
log.Printf("Command finished with error: %v", err)
When i run this code, this is the output i get
2014/12/10 07:49:39 Waiting for command to finish...
2014/12/10 07:49:39 Command finished with error: exit status 11
My C hello world program is
#include <stdio.h>
int main(){
printf("hello world");
}
The C code is compiled with gcc version 4.8.2 and executable is a.out.
The C executable a.out is a simple hello world program, i am getting the correct output, but i can't understand why exec.Wait() is exiting with status code 11 and not with 0.
The executable('a.out') is running normally when i run on a linux terminal without any faults or errors
答案1
得分: 4
我从GitHub上得到了答案这里。
它返回11是因为输出的长度是11,并且在我的Hello World程序中没有返回语句。所以当程序结束时,main函数从AX寄存器返回。
英文:
I got the answer from github here
It is returning 11 because the output length is 11 and there is no return statement in my hello world program. So main returns from AX register when it ended.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论