Compiler warnings for execvpe function

2019-09-12 06:47发布

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条回答
走好不送
2楼-- · 2019-09-12 07:07

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.

查看更多
登录 后发表回答