- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的程序。
#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/
我是错误处理的新手;在我的代码中,我需要测试函数的返回值并在发生错误时打印错误描述。 为了保持代码线程安全,我必须使用 strerror_r,但我有一些难以使用的地方。在以下代码中,发生了错误编号 2
这段代码安全吗: strerror_r(errcode,buffer,length); printf("Error: %s",buffer); 也就是说,如果缓冲区太小,我可以相信缓冲区是空终止的吗?
这是一个说明问题的简单程序。 #include #include int main () { char buf [30]; strerror_r (0, buf, sizeof (
OpenGroup POSIX.1-2001 定义 strerror_r , The Linux Standard Base Core Specification 3.1 也是如此.但是我找不到对错误
char *strerror_r(int errnum, char *buf, size_t buflen); 这些 buf/buflen 参数有什么用? 空缓冲区就像一个魅力: char* buf
我在这样的代码中为线程安全的 strerror_r 提供了一个 C++ 包装器: struct MyErrno {}; std::ostream& operator<<(std::stream& os
我在 Linux 中使用 libc 中的 strerror_r 函数。我用 0 作为缓冲区指针和缓冲区大小参数来调用它,它返回一个指向带有错误消息的字符串的指针。 我复制了一个字符串以供将来使用,但是
Android ndk-9c 似乎与 strerror_r 声明的标准相矛盾。 Linux 手册页显示 strerror_r 声明如下: int strerror_r(int errnum, char
VxWorks 提供了一个只接受两个参数的 strerror_r 版本。 STATUS strerror_r ( int errcode, /* error number */ cha
在测试期间,我有一个模拟对象,它设置了 errno = ETIMEDOUT; 我正在测试的对象看到错误并调用 strerror_r取回错误字符串: if (ret) { if (ret ==
我看到 strerror_r (...) Visual C++ 2008 不再支持 API,可能是因为线程安全问题。我想在我的程序中使用类似的功能。是否有任何其他 winapi 与 strerror_
我正在使用 strerror_r在日志记录辅助函数中。如手册页所述,此函数有两个版本。 POSIX 版本返回一个 int。 GNU 版本返回一个字符串 (char*)。 因此,为了让我的 C++ 代码
我如何获得 POSIX strerror_r而不是 GNU 版本? 我正在 Ubuntu 8.04 上使用 g++ 和 glibc 版本 2.7(基于中的内容)进行编译。 编辑 在上面的手册页上说:
这是我的程序。 #include #include #include #include int main() { char errbuf[256]; errno
我有一个 C 程序,它在 Linux 下编译和运行良好,没有任何警告,但是当试图在 SunOS 上编译它时,我收到以下警告: test.c: In function `my_function': te
在构建 OpenSSL-1.1.0d 时出现以下错误。 ./libcrypto.a(o_str.o):o_str.c:(.text+0x5cb): undefined reference to `st
我是一名优秀的程序员,十分优秀!