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

newbie picture newbie · Jun 29, 2015 · Viewed 8.8k times · Source

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!

Answer

missimer picture missimer · Jun 29, 2015

"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>