pistal
pistal

Reputation: 2456

Cannot execute binary file via execlp

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?

Upvotes: 0

Views: 812

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754950

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.

Upvotes: 2

jim mcnamara
jim mcnamara

Reputation: 16399

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.

Upvotes: 0

Related Questions