0

I am not able to execute a binary via execlp.

chdir("/home/foo/bar/baz/MB/"); execlp("bash", "bash", "./foobarbaz 1", NULL); 

foobarbaz is my c file and I get the following error:

./foobarbaz: cannot execute binary file 

I tried doing chmod +x foobarbaz.c

and also test.c the file in which execlp is present.

What is the mistake I am making?

4
  • "foobarbaz is my c file" really? do you mean it is your compiled executable or is it actually a source code file? Commented Jun 19, 2013 at 20:39
  • the extension is .c Commented Jun 19, 2013 at 20:42
  • In bash, can you cd /home/foo/bar/baz/MB/ and ./foobarbaz 1? Commented Jun 19, 2013 at 20:56
  • yes. In bash i can do that directly. @johnathan was correct. Commented Jun 19, 2013 at 21:00

2 Answers 2

2

You can run the binary directly:

execlp("./foobarbaz", "./foobarbaz", "1", (char *)0); 

The shell is used to execute shell scripts (at least when you say bash ./foobarbaz 1); your binary isn't a shell script.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a question to add to that: Does it mean, that the shell is not returned till the execution is done?
When you use execlp(), there is no shell involved; the process (your ./foobarbaz program) replaces the current program and the process terminates when ./foobarbaz exits. If you had invoked it via the shell (bash, say), you might have written: execlp("bash", "bash", "-c", "./foobarbaz 1", (char *)0); and then the shell you launched would in turn launch ./foobarbaz, and the shell would wait for ./foobarbaz to complete before exiting itself. But it would mean that bash had replaced the current process instead of ./foobarbaz, of course.
0

When you compile a C file - like foo.c you get an output binary

cc foo.c 

gives ./a.out as the binary file

cc foo.c -o foo 

gives ./foo as the binary file

foo.c is not executable.

1 Comment

I did it give it as ./foo 1 and not foo.c 1 , right? why is it still an error?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.