编译时出现以下警告:
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 man page 中所述
以下是我的代码的相关部分:
#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;
}
有人知道为什么我总是收到这个错误吗?谢谢!
最佳答案
“函数的隐式声明”意味着编译器没有看到该函数的声明。大多数编译器,包括 gcc,都会假定函数的使用方式是正确的并且返回类型是 int
。这通常是个坏主意。即使您正确使用了参数,它仍然会抛出此错误,因为编译器不知道您是否正确使用了参数。只有在包含 unistd.h 之前定义了 _GNU_SOURCE
时,才会包含 execvpe
的声明,因为它是 GNU 扩展。
你会想要这样的东西:
#define _GNU_SOURCE
#include <unistd.h>
关于c - 尝试使用 execvpe(...) 但出现隐式声明错误 - 尽管我认为我使用的是正确的参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31106601/