Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 975
newbie Avatar asked Jun 29 '15 01:06

newbie


1 Answers

"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>
like image 119
missimer Avatar answered Oct 20 '22 16:10

missimer