2017-04-12 148 views
0

我在c中测试execvpe(),我尝试了下面的代码,它导致错误,因为“隐式声明函数'execvpe'在C99 [-Wimplicit-function-declaration]中无效” 。execvpe隐式声明错误

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define _GNU_SOURCE 
#include <unistd.h> 

int main(int argc, char const *argv[]) 
{ 
    //execl("/bin/echo", "echo", "Hello, world", NULL); 
    char *path = getenv("PATH"); 
    char pathenv[strlen(path) + sizeof("PATH=")]; 
    sprintf(pathenv, "PATH=%s", path); 
    char *envp[] = {pathenv, NULL}; 
    char *tests[] = {"ls", "-lR", NULL}; 
    execvpe(tests[0], tests, envp); 
    fprintf(stderr, "failed to execute \"%s\"\n", tests[0]); 
    return 0; 
} 

然后我如下测试此代码测试现存的状态(这是我从Compiler warnings for execvpe function复制,这一次没有错误。是否有任何人可以帮助我弄清楚什么是错在我上面的代码?谢谢!

#include <unistd.h> 
extern int execvpe(const char *file, char *const argv[], char *const envp[]); 

回答

0
之前任何 #include指令

移动#define _GNU_SOURCE指令,例如

#define _GNU_SOURCE 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 

glibc中,所有这些头拉入features.h,它根据_XOPEN_SOURCE,_POSIX_SOURCE,_GNU_SOURCE等的设置设置各种宏。在第一次包括时,它没有被设置。当你开始使用unistd.h时,features.h已经包含在内,并且不会再应用。

+0

非常感谢!在我移动它后,它有错误,如“错误:函数的隐式声明'execvpe'在C99中是无效的[-Werror,-Wimplicit-function- declaration] execvpe(tests [0],tests,envp);” – coco

+0

@coco它在这里与GCC 6.3.1和Glibc 2.25一起工作。你是否可能使用其他一些不提供'execvpe'的C标准库? – ephemient

+0

你可以在这里看到缺少警告的演示:https://godbolt.org/g/m2eyRi – ephemient