带有std=c11参数的GCC警告

带有std=c11参数的GCC警告,c,gcc,pthreads,c11,C,Gcc,Pthreads,C11,下面是一个使用pthread_kill()调用的小C源代码: 我得到一个隐式声明: main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration] 带-std=c99 与-std=c11的结果相同: main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplic

下面是一个使用pthread_kill()调用的小C源代码:

我得到一个隐式声明:

main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
带-std=c99 与-std=c11的结果相同:

main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
带-std=c90 它只是简单地工作,没有任何错误/警告


感谢您的反馈。

如果您使用Posix功能,则需要定义适当的功能测试宏。请参阅
man功能\u测试\u宏

如果未将
\u POSIX\u C_SOURCE
定义为适当的值(取决于所需的最低POSIX级别),则来自该标准和后续POSIX标准的接口将不会由标准库标题定义

例如,如果需要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;
}
#定义POSIX_C_SOURCE200809L
#包括
#包括
#包括
int main(int argc,char*argv[])
{
pthread_t th=NULL;
pthread_kill(th,0);
返回0;
}

你是说
-lpthread
而不是
-pthread
?@SouravGhosh它们完全一样。嗯,实际上应该是首选。另一件需要注意的事情是
-std=gnu99
或例如
-std=gnu11
标志,它为您提供了相应C标准的扩展,如果您在linux上使用glibc,它包括库功能,例如posix函数。@不,实际上我对GNU扩展不感兴趣,我更喜欢使用严格的ISO规范编译(示例中我删除了-pedantic*args)。@mafso确定。但不管名称是“gnu”,事实是如果您在linux上使用glibc,-std=gnu99将启用posix接口。你说的核心语言不同是完全正确的。谢谢你,这就是解决方案。但是,我怎么知道这个函数是Posix特性呢?pthread_create()也是Posix(1.2001),但我从未指定过它。根据您的文档,我没有看到pthread_kill()手册页中提到“feature_test_macros”。@Flow-我想重点是,pthreads不是ISO C的一部分。“我怎么知道这个函数是Posix功能?”-pthreads是“Posix线程”的缩写。有些奇怪的是,包含头并没有公开功能,但pthreads在某些情况下仍然会影响编译器选项/链接。@Flow:大多数手册页都有一节介绍功能测试宏;如果pthread_create在您的系统中缺少一个,我会说这是一个文档错误。但是,您可以使用posix标准中链接在答案中的图表,从符合行的
中导出值。我只是一直在使用200809L(我把
-D_POSIX_C_SOURCE=200809L
放在我的Makefile的C标志中),但准确地说,它可能“更好”。
gcc main.c -std=c99 -pthread
main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
gcc main.c -std=c90 -pthread
#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;
}