5

编译时收到以下警告:

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

^

我的理解是,当您尝试使用的函数具有不正确的参数类型时,就会发生这种情况。但是,我很确定我提供了正确的论点:

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

Linux 手册页中所述

以下是我的代码的相关部分:

#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;

}

任何人都知道为什么我不断收到这个错误?谢谢!

4

1 回答 1

12

“函数的隐式声明”意味着编译器没有看到该函数的声明。大多数编译器,包括 gcc,都会假设函数的使用方式是正确的并且返回类型是int. 这通常是一个坏主意。即使您正确使用了参数,它仍然会抛出此错误,因为编译器不知道您正在正确使用参数。execvpe仅当在包含 unistd.h 之前定义时才包含声明,_GNU_SOURCE因为它是 GNU 扩展。

你会想要这样的东西:

#define _GNU_SOURCE
#include <unistd.h>
于 2015-06-29T02:20:01.837 回答