perror 手册页中的 SYNOPSIS 部分是什么意思?
What does SYNOPSIS part in perror man page mean?
perror
的 man page 中的 SYNOPSIS 部分是:
#include <stdio.h>
void perror(const char *s);
#include <errno.h>
const char * const sys_errlist[];
int sys_nerr;
int errno; /* Not really declared this way; see errno(3) */
根据man page specification,SYNOPSIS 部分表明
For functions, it shows any required data declarations or #include directives, followed by the function declaration.
以下代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ls_args[2] = {"nonsense", NULL};
execv(ls_args[0], ls_args); // no return
perror("execve failed");
return 2;
}
输出错误信息execve failed: No such file or directory
,对应的errno
为2
由于errno
是errno.h
中定义的全局变量(实际上是一个宏),不包含errno.h
头,这段代码如何触发errno
修改?
摘要部分的 #include <errno.h> ... int errno;
是什么意思?似乎 perror()
可以在没有这部分代码的情况下调用,谢谢!
根据 C 标准,宏 errno
在 errno.h
中声明,如果要编写使用 [=10= 的可移植程序,则必须显式包含 errno.h
].联机帮助页概要告诉您这一点。 (这并不是说您需要包含 errno.h
才能使用 perror
。有时概要部分会告诉您其他相关的图书馆设施。)
标准中没有任何内容指定 errno
宏的定义是什么,或者您可以在实现中的确切位置找到该定义的扩展引用的任何对象。 perror
的实现显然需要能够访问 errno
所指的任何对象,但由于它不需要可移植,因此完全没有说明它是如何工作的。
特别是,最近的 C 标准要求对象 errno
指的是 thread-local,这样每个线程都有自己的 errno
对象. (如果不是这种情况,该机制在多线程代码中基本上不可用。)标准也没有指定线程本地存储的精确实现,并且在特定实现上它可能映射到由底层操作系统。
perror
的 man page 中的 SYNOPSIS 部分是:
#include <stdio.h>
void perror(const char *s);
#include <errno.h>
const char * const sys_errlist[];
int sys_nerr;
int errno; /* Not really declared this way; see errno(3) */
根据man page specification,SYNOPSIS 部分表明
For functions, it shows any required data declarations or #include directives, followed by the function declaration.
以下代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ls_args[2] = {"nonsense", NULL};
execv(ls_args[0], ls_args); // no return
perror("execve failed");
return 2;
}
输出错误信息execve failed: No such file or directory
,对应的errno
为2
由于errno
是errno.h
中定义的全局变量(实际上是一个宏),不包含errno.h
头,这段代码如何触发errno
修改?
摘要部分的 #include <errno.h> ... int errno;
是什么意思?似乎 perror()
可以在没有这部分代码的情况下调用,谢谢!
根据 C 标准,宏 errno
在 errno.h
中声明,如果要编写使用 [=10= 的可移植程序,则必须显式包含 errno.h
].联机帮助页概要告诉您这一点。 (这并不是说您需要包含 errno.h
才能使用 perror
。有时概要部分会告诉您其他相关的图书馆设施。)
标准中没有任何内容指定 errno
宏的定义是什么,或者您可以在实现中的确切位置找到该定义的扩展引用的任何对象。 perror
的实现显然需要能够访问 errno
所指的任何对象,但由于它不需要可移植,因此完全没有说明它是如何工作的。
特别是,最近的 C 标准要求对象 errno
指的是 thread-local,这样每个线程都有自己的 errno
对象. (如果不是这种情况,该机制在多线程代码中基本上不可用。)标准也没有指定线程本地存储的精确实现,并且在特定实现上它可能映射到由底层操作系统。