c - 带有 std=c11 arg 的 GCC 警告

标签 c gcc pthreads c11

这里是一些使用 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_macrosPosix 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/

相关文章:

c - 一维统计程序

c - 如何计算数组中相似数字的数量

c - 问 : Arrays and Functions Homework

c - 不带参数的 pthread_create 函数

c++ - 如何在 C/C++ 中处理 unicode 字符序列?

c++ - clang: error: linker command failed with exit code 1 (使用 -v 查看调用) MINIX3

c - GDB 无法在共享库上设置断点

c++ - 通过常量迭代器从 STL 容器中删除

c++ - 如何提高使用屏障的 C++ pthread 代码的时间性能

PHP pthreads : Fatal error: Class 'Thread' not found