What does '_IO(…)' mean in C headers in Linux?

怎甘沉沦 提交于 2019-12-01 01:02:13

问题


I have a Linux standard header file e.g.

/usr/src/linux-headers-3.2.0-35/include/linux/usbdevice_fs.h

which contain define statements as follows:

#define USBDEVFS_SUBMITURB32       _IOR('U', 10, struct usbdevfs_urb32)
#define USBDEVFS_DISCARDURB        _IO('U', 11)
#define USBDEVFS_REAPURB           _IOW('U', 12, void *)

What does '_IOR', '_IO' and '_IOW' mean? What value is actually given e.g. to USBDEVFS_DISCARDURB?


回答1:


They define ioctl numbers, based on ioctl function and input parameters. The are defined in kernel, in include/asm-generic/ioctl.h.

You need to include <linux/ioctl.h> (or linux/asm-generic/ioctl.h) in your program. Before including
/usr/src/linux-headers-3.2.0-35/include/linux/usbdevice_fs.h

You can't "precompile" this values (e.g. USBDEVFS_DISCARDURB), because they can be different on other platforms. For example, you are developing your code on plain old x86, but then someone will try to use it on x86_64/arm/mips/etc. So you should always include kernel's ioctl.h to make sure, you are using right values.




回答2:


These are also macros defined elsewhere.

In general if you want to see your code after pre-processor has been computed use

gcc -E foo.c

this will output your code pre-processed

For example:

foo.c

#define FORTY_TWO 42

int main(void)
{
  int foo = FORTY_TWO;
}

will give you with gcc -E foo.c:

int main(void)
{
  int foo = 42;
}


来源:https://stackoverflow.com/questions/14626867/what-does-io-mean-in-c-headers-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!