gpt4 book ai didi

c - 为什么 strerror_r 在使用 gnu90 和 c90 标准编译时表现不同?

转载 作者:行者123 更新时间:2023-12-01 23:15:05 25 4
gpt4 key购买 nike

这是我的程序。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

int main()
{
char errbuf[256];

errno = 0;
strtoul("99999999999999999999999999999999999999999999", NULL, 0);
strerror_r(errno, errbuf, sizeof errbuf);
printf("strerror_r: %s\n", errbuf);

return 0;
}

当我使用 -std=gnu90-std=gnu99 编译它时,我得到了预期的输出。

susam@nifty:~/lab/linux$ rm -f a.out && gcc -std=gnu90 -Wall -Wextra -pedantic foo.c && ./a.out 
strerror_r: Numerical result out of range
susam@nifty:~/lab/linux$ rm -f a.out && gcc -std=gnu99 -Wall -Wextra -pedantic foo.c && ./a.out
strerror_r: Numerical result out of range

但是当我用 -std=c90-std=c99 编译它时,我收到警告并且没有看到 strerror_r 将字符串放入 errbuf

lone@debian:~/lab/linux$ rm -f a.out && gcc -std=c90 -Wall -Wextra -pedantic foo.c && ./a.out
foo.c: In function ‘main’:
foo.c:12:2: warning: implicit declaration of function ‘strerror_r’ [-Wimplicit-function-declaration]
strerror_r(errno, errbuf, sizeof errbuf);
^
strerror_r:
lone@debian:~/lab/linux$ rm -f a.out && gcc -std=c99 -Wall -Wextra -pedantic foo.c && ./a.out
foo.c: In function ‘main’:
foo.c:12:2: warning: implicit declaration of function ‘strerror_r’ [-Wimplicit-function-declaration]
strerror_r(errno, errbuf, sizeof errbuf);
^
strerror_r:

当我使用 -std=c90-std=c99 时出了什么问题?

最佳答案

使用 -std=c89,您要求实现专门提供 ISO 9899:1989 标识符部分的声明。标识符 strerror_r 不是 C89(或 C99)的一部分,因此没有原型(prototype)。因此,您会收到有关隐式声明的警告。

如果您查看相关的 header ,您可能会发现 strerror_r 原型(prototype)隐藏在 #ifdef 的迷宫中。 -std 选项更改影响原型(prototype)可见性的预定义宏集。

关于c - 为什么 strerror_r 在使用 gnu90 和 c90 标准编译时表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com