C++ errnos的定义在哪里?示例linux c/c++;i2c程序

C++ errnos的定义在哪里?示例linux c/c++;i2c程序,c++,c,linux,errno,i2c,C++,C,Linux,Errno,I2c,当一个经典的LinuxC/c++软件出现问题时,我们有一个神奇的变量errno,它为我们提供了一个刚刚出错的线索 但这些错误的定义是什么 让我们举一个例子(它实际上是来自Qt应用程序的一部分,因此是qDebug() 我的问题是在哪里找到“9”代表的错误? 我不喜欢我代码中的那种神奇数字 /谢谢它们通常在/usr/include/errno.h*中定义,可以通过包含errno.h来访问: #include <errno.h> 但是,在Linux shell中,您可以使用perror实

当一个经典的LinuxC/c++软件出现问题时,我们有一个神奇的变量errno,它为我们提供了一个刚刚出错的线索

但这些错误的定义是什么

让我们举一个例子(它实际上是来自Qt应用程序的一部分,因此是qDebug()

我的问题是在哪里找到“9”代表的错误? 我不喜欢我代码中的那种神奇数字


/谢谢

它们通常在
/usr/include/errno.h
*中定义,可以通过包含errno.h来访问:

#include <errno.h>
但是,在Linux shell中,您可以使用
perror
实用程序找出不同的errno值的含义:

$ perror 9
OS error code   9:  Bad file descriptor
编辑: 您的代码应更改为使用符号值:

if (err == EBADF)
{
    // do something...
}
else 
{
    //do someting else
}
编辑2:*至少在Linux下,实际值是在
/usr/include/asm generic/{errno,errno base}.h
和其他地方定义的,如果您想查看这些值,就很难找到它们。

NAME
NAME
       errno - number of last error

SYNOPSIS
       #include <errno.h>

DESCRIPTION
       The  <errno.h>  header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate
       what went wrong.  Its value is significant only when the return value of the call indicated an error (i.e., -1 from most system calls; -1 or NULL from  most
       library functions); a function that succeeds is allowed to change errno.

       Valid error numbers are all non-zero; errno is never set to zero by any system call or library function.
errno-上次错误的数目 提要 #包括 描述 头文件定义整数变量errno,在发生错误时由系统调用和一些库函数设置,以指示 出了什么问题。它的值只有在调用的返回值指示错误时才有效(即,大多数系统调用为-1;大多数系统调用为-1或NULL) 图书馆职能);成功的函数允许更改errno。 有效错误号均为非零;任何系统调用或库函数都不会将errno设置为零。
文件是:

/usr/include/errno.h


--p

通常要实现错误处理,您需要知道函数可以返回哪些特定的错误代码。此信息可在man或上获得

例如,对于ioctl调用,您应该预期以下代码:

   EBADF  d is not a valid descriptor.

   EFAULT argp references an inaccessible memory area.

   EINVAL Request or argp is not valid.

   ENOTTY d is not associated with a character special device.

   ENOTTY The specified request does not apply to the kind of object that the
          descriptor d references.
编辑:
如果包含
,那么定义所有可能错误代码的所有文件也都包含在内,因此您不需要知道它们的确切位置。

试试看,这正是我要查找的,但是我在哪里可以找到EBADF?如果我grep EBADF/usr/include/errno.h,我在头文件中找不到类似的内容。。。它必须被隐藏几步??对于ioctl的特定情况+1,糟糕的是,你不能接受2个答案。
if (err == EBADF)
{
    // do something...
}
else 
{
    //do someting else
}
NAME
       errno - number of last error

SYNOPSIS
       #include <errno.h>

DESCRIPTION
       The  <errno.h>  header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate
       what went wrong.  Its value is significant only when the return value of the call indicated an error (i.e., -1 from most system calls; -1 or NULL from  most
       library functions); a function that succeeds is allowed to change errno.

       Valid error numbers are all non-zero; errno is never set to zero by any system call or library function.
   EBADF  d is not a valid descriptor.

   EFAULT argp references an inaccessible memory area.

   EINVAL Request or argp is not valid.

   ENOTTY d is not associated with a character special device.

   ENOTTY The specified request does not apply to the kind of object that the
          descriptor d references.