这里是一些使用 pthread_kill() 调用的 C 源代码:
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
int main(int argc, char *argv[])
{
pthread_t th = NULL;
pthread_kill(th, 0);
return 0;
}
Gcc 编译根据 -std 参数值产生不同的结果(见下文)。我不明白这些不同的行为。 除了 pthread_kill() 符合 POSIX.1-2008 标准外,我没有在手册页中获取有趣的信息。
环境:Linux 3.2 64 位。海湾合作委员会 4.7.2。
使用-std=c11
gcc main.c -std=c11 -pthread
我得到一个隐式声明:
main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
使用-std=c99
gcc main.c -std=c99 -pthread
与 -std=c11 相同的结果:
main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
使用-std=c90
gcc main.c -std=c90 -pthread
它可以正常工作,没有任何错误/警告。
感谢您的反馈。
最佳答案
如果您使用 Posix 功能,则需要定义适当的功能测试宏。请参阅 man feature_test_macros
或 Posix standard .
如果您没有将 _POSIX_C_SOURCE
定义为适当的值(取决于您需要的最低 Posix 级别),那么来自该标准和后续 Posix 标准的接口(interface)将不会由标准库 header 定义。
如果您需要 Posix.1-2008,例如,您需要这样做:
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
int main(int argc, char *argv[])
{
pthread_t th = NULL;
pthread_kill(th, 0);
return 0;
}
关于c - 带有 std=c11 arg 的 GCC 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459245/