douhan4812 2014-12-10 08:25
浏览 158

golang exec.Command无法运行C二进制文件

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条回答 默认 最新

  • dongqiangteng7319 2014-12-10 15:25
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页