Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec.Command couldn't C executables , throwing exit status errors #9237

Closed
domitian opened this issue Dec 10, 2014 · 2 comments
Closed

exec.Command couldn't C executables , throwing exit status errors #9237

domitian opened this issue Dec 10, 2014 · 2 comments

Comments

@domitian
Copy link

hi,
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 simple

 #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

@rsc
Copy link
Contributor

rsc commented Dec 10, 2014

This isn't a Go problem.

Your C program does not contain a return statement. If you build it with 'gcc -Werror' it should tell you this, I would think. In any case, because there is no return statement, main returns whatever is in register AX when it ends. That turns out to be the return value from printf, the length of "hello world", 11.

When you run the program on a Linux terminal you are getting exactly the same behavior, but you are ignoring it. Try:

$ ./a.out
$ echo $?

and it will say 11.

@rsc rsc closed this as completed Dec 10, 2014
@domitian
Copy link
Author

thank you very much, i wasted a whole day on this issue.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants