Attempting to use execvpe(...) but get implicit declaration error - even though I think I'm using the correct argument types
Asked Answered
C

1

7

I am getting the following warning when I compile:

execute.c:20:2: warning: implicit declaration of function ‘execvpe’[-Wimplicit-function-declaration] execvpe("ls", args, envp);

^

My understanding is that this occurs when the function you are trying to use has the incorrect types of arguments. However, I am pretty sure that I am supplying the correct arguments to:

int execvpe(const char *file, char *const argv[], char *const envp[]);

as described in the Linux man page

Here are the relevant parts of my code:

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>

void test_execvpe(char* envp[])
{
    const char* temp = getenv("PATH");
    char path[strlen(temp)+1];
    strcpy(path, temp);
    printf("envp[%d] = %s\n", 23, envp[23]); //print PATH
    char* args[] = {"-l", "/usr", (char*) NULL};
    execvpe("ls", args, envp);
}

int main( int argc, char* argv[], char* envp[])
{

    //test_execlp();
    test_execvpe(envp);
    return 0;

}

Anyone know why I keep getting this error? Thanks!

Clathrate answered 29/6, 2015 at 1:37 Comment(8)
Add #define _GNU_SOURCE before the #include of unistd.h.Spanish
Thanks for the response. I will look this up. Do you have quick explanation as to why this is needed?Clathrate
It's a GNU extension. It is mentioned on the man page.Spanish
One more followup question for you. The argument "-l" does not seem to be added to the command "ls" - the output is as if I typed "ls /usr" in the command shell. bin games include lib local sbin share src It just skips over the "-l"Clathrate
The first arg in args should be argv[0]. Use something like "{"ls" , "-l", "/usr", (char*) NULL}"Spanish
Hmm, thanks for that! I remember reading about that yesterday. I should have made the connection. Last question: How you know all this off the top of your head? Thanks!Clathrate
Smile. I started using Unix with V7. Slashdot is getting mad at me for extended comments. V7 was a long time ago.Spanish
And of course by slashdot I meant Stackoverflow.Spanish
G
15

"implicit declaration of function" means the compiler has not seen a declaration for that function. Most compilers, including gcc, will assume that the way the function has been used is correct and the return type is an int. This is generally a bad idea. Even if you use the arguments correctly it will still throw this error as the compiler does not know you are using the arguments correctly. The declaration of execvpe is only included if _GNU_SOURCE is defined before including unistd.h as it is a GNU extension.

You will want something like:

#define _GNU_SOURCE
#include <unistd.h>
Genethlialogy answered 29/6, 2015 at 2:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.