Compiler warnings for execvpe function

爷,独闯天下 提交于 2019-12-13 06:46:24

问题


I have a program written in c which uses the execvpe(3) function, and I've got a line set to include the requisite header file:

#include <unistd.h>

I compile this file with the following command...

gcc foo.c -o foo

...only to get the following warning:

warning: implicit declaration of function ‘execvpe’ [-Wimplicit-function-declaration]

I've encountered similar behavior with files that reference the pthread_create(3) function. The difference is obviously that whereas the pthread_create(3) man page clearly states that one should "Compile and link with -pthread", the man page for the exec(3) family of functions does not have any such instructions. Furthermore, I cannot find any reference in the manual or online to an analogous compiler flag for the exec(3) family.

I'd appreciate any information you have on this matter. If there is some flag I should be using at compile time, or if I am looking in entirely the wrong place for a solution, please let me know.


回答1:


The man page here states that it is necessary to define the _GNU_SOURCE feature test macro to enable the function declaration:

#define _GNU_SOURCE
#include <unistd.h>

Interestingly however the link to unistd.h on the same man page takes you to an implementation that does not declare execvpe at all. You could check your system's unistd.h file to check that it is declared and is dependent on _GNU_SOURCE - that is to solve this an similar problems in the future - check the header content to see if it is even there and what macros it may depend on.

If it is not in the header file, then it is most probably also not in the library, but you could check as follows:

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

which will satisfy the compiler, but if you then get a linker error, then the function is simply not included in the library in any case.



来源:https://stackoverflow.com/questions/37241835/compiler-warnings-for-execvpe-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!