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?

Community
  • 1
  • 1
pistal
  • 2,310
  • 13
  • 41
  • 65

2 Answers2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Just a question to add to that: Does it mean, that the shell is not returned till the execution is done? – pistal Jun 19 '13 at 20:34
  • 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. – Jonathan Leffler Jun 19 '13 at 21:05
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.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51