errno变量问题

OOPhaisky 2006-11-24 05:26:53
我想问一下,errno全局变量是在哪个文件中“定义”的?
注意,我说的是“定义”,而非“声明”,因为errno的声明我已经在一个头文件中找到了(好像是errno.h,记不清了):
extern int errno;
但是没有找到“定义”。

我想,是不是操作系统在加载每个程序的时候为每个程序定义的这个全局errno变量?

望指教,谢谢。
...全文
1164 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hu_zy 2006-12-13
  • 打赏
  • 举报
回复
gcc不同的版本定义不一样的,是一个易失变量
你#include <errno.h>就可以了不要再自己extern int errno;
画蛇添足。
hqx8211 2006-12-04
  • 打赏
  • 举报
回复
标准库中实现.对于gcc来讲,应该就是在glibc中实现的定义.
OOPhaisky 2006-12-02
  • 打赏
  • 举报
回复
to hqx8211() :
"extern int errno声明"仅仅是一个“声明”啊,我的意思是“定义”,即“int errno”,而不是“extern...”?
evawu999 2006-11-27
  • 打赏
  • 举报
回复
extern int errno找不到?用的什么操作系统?
hqx8211 2006-11-27
  • 打赏
  • 举报
回复
简单的说,errno在标准C中是一个整型变量,在errno.h中声明,C标准库中实现。多线程技术中,为了使errno线程安全,使用宏定义替代了简单的extern int errno声明。man errno, 再看看C99标准文档,就明白了。

---选自/usr/include/bits/errno.h------
//当使用多线程时,errno是个宏定义
# if !defined _LIBC || defined _LIBC_REENTRANT
/* When using threads, errno is a per-thread value. */
# define errno (*__errno_location ())
# endif
# endif /* !__ASSEMBLER__ */
#endif /* _ERRNO_H */

----选择/usr/include/errno.h-------
//标准的errno定义,在errno.h中声明,libc标准库中实现定义。
/* Declare the `errno' variable, unless it's defined as a macro by
bits/errno.h. This is the case in GNU, where it is a per-thread
variable. This redeclaration using the macro still works, but it
will be a function declaration without a prototype and may trigger
a -Wstrict-prototypes warning. */
#ifndef errno
extern int errno;
#endif

以下是C99标准对errno.h的说明:(从PDF中拷的,格式混乱,凑合看一下....)
7.5 Errors<errno.h>1The header <errno.h> de?nes several macros, all relating to the reporting of errorconditions.2The macros areEDOMEILSEQERANGEwhich expand to integer constant expressions with typeint,distinct positive values, andwhich are suitable for use in#ifpreprocessing directives; anderrnowhich expands to a modi?able lvalue169)that has typeint,the value of which is set to apositive error number by several library functions. It is unspeci?ed whethererrnois amacro or an identi?er declared with external linkage. If a macro de?nition is suppressedin order to access an actual object, or a program de?nes an identi?er with the nameerrno,the behavior is unde?ned.3The value of errno is zero at program startup, but is never set to zero by any libraryfunction.170)The value of errno may be set to nonzero by a library function callwhether or not there is an error, provided the use of errno is not documented in thedescription of the function in this International Standard.4Additional macro de?nitions, beginning with E and a digit or E and an uppercaseletter,171)may also be speci?ed by the implementation.
OOPhaisky 2006-11-25
  • 打赏
  • 举报
回复
to hqx8211() :

能否给我举一个具体的例子,比如说是宏定义,那么能不能给我一个例子阿?谢谢。
hqx8211 2006-11-24
  • 打赏
  • 举报
回复
每个线程一个errno,而不是程序(或者准确的说,进程)。C标准中说明了它的规范,由编译器实现。可能由宏定义来实现。

The integer errno is set by system calls (and some library functions) to indicate what went wrong. Its value
is significant only when the call returned an error (usually -1), and a library function that does succeed is
allowed to change errno.

Sometimes, when -1 is also a legal return value one has to zero errno before the call in order to detect possi-
ble errors.

errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly
declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in
any other thread.

Valid error numbers are all non-zero; errno is never set to zero by any library function. All the error names
specified by POSIX.1 must have distinct values.

POSIX.1 (2001 edition) lists the following symbolic error names. Of these, EDOM and ERANGE are in the ISO C
standard. ISO C Amendment 1 defines the additional error number EILSEQ for coding errors in multibyte or wide
characters.
OOPhaisky 2006-11-24
  • 打赏
  • 举报
回复
由谁定义?
操作系统,还是编译器?
什么叫做“被使用时定义”?
无知者无谓 2006-11-24
  • 打赏
  • 举报
回复
应该是在被使用(设置)的时候定义的

23,130

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧