4

i've been trying to use execvp to run a c program but it always seems to fail.

from main.c

int main() {
char* args[] = {"2", "1"};
    if(execvp("trial.c", args) == -1) {
        printf("\nfailed connection\n");
    }

from trial.c

int main(int argc, char** argv){
printf("working");
return 1;
}

I think i tried every way to possibly represent that file location in the exec() and it always results in "failed connection".

5
  • 3
    It looks like you're trying to execute something that is not (yet) an executable? Commented Sep 13, 2018 at 12:52
  • Have you tried executing 'trial.c' from the command line to see if it possibly could work? Commented Sep 13, 2018 at 13:03
  • How do you compile those two C source files, .c files? Commented Sep 13, 2018 at 13:06
  • Doing exactly what lead to you getting "failed connection"? Commented Sep 13, 2018 at 13:07
  • "if(execvp(...))" No need for the if(), as the members of the exec*() family of functions only return on error. Commented Sep 13, 2018 at 13:10

4 Answers 4

2

The first parameter to execvp expects the name of an executable file. What you've passed it is the name of a source file. You need to first compile trial.c, then pass the name of the compiled executable to execvp.

Regarding the second parameter to execvp, the last element in the array must be NULL. That's how it knows it reached the end of the list. Also, by convention the first parameter to a program is the name of the program itself.

So first compile trial.c:

gcc -g -Wall -Wextra -o trial trial.c

Then modify how to call it in main.c:

int main() {
    char* args[] = { "trial", "2", "1", NULL };
    if(execvp("trial", args) == -1) {
        printf("\nfailed connection\n");
    return 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for responding! I did the changes you mentioned but i am still getting "failed connection". did anything need to be changed on the trail.c file? my current main.c is: char* args[] = {"player", NULL}; if(execvp("player", args) == -1) { printf("\nfailed connection\n"); }
@josephcornell Use perror instead of printf to print the error messages. That will tell you why it failed.
1

First argument for execvp is path to executable. You need to build the executable for trial.c and pass the path of the executable to execvp.

if(execvp("---path to executable---/ExecTrial", args) == -1) {
    printf("\nfailed connection\n");
}

If you don't pass the executable path, execvp will search the executable in the colon-separated list of directory pathnames specified in the PATH environment variable.

Comments

0

trial.c is not a valid executable file. C is not a scripting or interpreted language; you cannot run C source files directly. A C program must be compiled and linked into an executable.

Comments

0

If you're trying to call execvp() on a source file then this is not what this function does. The first argument is expected to be a path of an executable. If you want to run the program which trial.c is the source of, you should build (compile and such) it first. For example like this:

$ gcc -o trial trial.c 

Then call execvp() on the your newly created executable instead of the source file:

if(execvp("trial", args) == -1) { //...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.