[英]How compile a user space program with kernel headers
I need to test a usbfs device by using ioctl
. 我需要使用
ioctl
测试一个usbfs设备。 I need use ioctl(fd, USBDEVFS_REAPURBNDELAY, &receive);
我需要使用
ioctl(fd, USBDEVFS_REAPURBNDELAY, &receive);
. 。 But
USBDEVFS_REAPURBNDELAY
is defined in a kernel header, so I include some kernel file like that: 但
USBDEVFS_REAPURBNDELAY
是在内核头文件中定义的,所以我包含了一些内核文件:
#include <linux/usb.h>
#include <linux/usbdevice_fs.h>
#include <asm/byteorder.h>
#include <linux/usb/ch9.h>
and compile it with gcc -g -I /usr/src/linux-3.4.4-2-pae/include/ -o /home/madper/kernel/usb/select_for_usb /home/madper/kernel/usb/select_for_usb.c
并使用
gcc -g -I /usr/src/linux-3.4.4-2-pae/include/ -o /home/madper/kernel/usb/select_for_usb /home/madper/kernel/usb/select_for_usb.c
编译
It said #warning "Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders" [-Wcpp]
它说
#warning "Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders" [-Wcpp]
And folled by lots of errors, so, What's the right way to use kernel headers? 并且有很多错误,所以,使用内核头文件的正确方法是什么?
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:21:2: error: unknown type name ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:36:2: error: unknown type name ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:37:33: error: ‘kernel_ulong_t’ undeclared here (not in a function)
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:37:3: error: requested alignment is not an integer constant
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:119:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:141:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:154:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:168:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:179:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:187:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:195:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:200:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:227:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:260:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:314:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:341:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:366:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:413:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:433:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:444:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:479:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:508:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:540:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:551:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:566:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:582:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/home/madper/kernel/usb/select_for_usb.c: In function ‘main’:
/home/madper/kernel/usb/select_for_usb.c:46:30: error: expected expression before ‘void’
You really shouldn't have to include kernel headers directly. 你真的不应该直接包含内核头文件。 In some cases it is necessary and probably the best way to handle it is to grab a copy of the kernel headers and hack out just the parts you need.
在某些情况下,这是必要的,并且可能最好的方法是获取内核头文件的副本并仅删除所需的部分。 But in this case I'm not sure I understand why you need kernel headers.
但在这种情况下,我不确定我理解为什么你需要内核头文件。 Why doesn't it work just to include
<linux/usbdevice_fs.h>
? 为什么不包含
<linux/usbdevice_fs.h>
?
On all the systems I looked at (a couple of different Ubuntu systems and one RHEL 6 system) that file can be included from userspace and has the define of USBDEVFS_REAPURBNDELAY
. 在我查看的所有系统(几个不同的Ubuntu系统和一个RHEL 6系统)上,该文件可以包含在用户空间中,并具有
USBDEVFS_REAPURBNDELAY
的定义。 I tried 我试过了
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main()
{
printf("USBDEVFS_REAPURBNDELAY is %ld\n", USBDEVFS_REAPURBNDELAY);
return 0;
}
and I can build it cleanly and when I run it I get 我可以干净利落地建造它,当我运行时,我得到它
USBDEVFS_REAPURBNDELAY is 1074287885
so at least based on what you wrote above, you don't need any kernel headers for that ioctl define. 所以至少根据你上面写的内容,你不需要ioctl定义的任何内核头文件。 I suspect anything else you think you need for userspace USB stuff is similarly exported to userspace in a clean header.
我怀疑你认为你需要的任何其他用户空间USB东西同样导出到一个干净的标题中的用户空间。
You can't use kernel headers for user programs. 您不能将内核头文件用于用户程序。 Even if you do by some means it is strongly discouraged.
即使你通过某种方式做到这一点,也强烈劝阻。 Check the below link for more clarification.
请查看以下链接以获得更多说明。
http://lwn.net/Articles/113349/ http://lwn.net/Articles/113349/
If you do want to control usb devices from user space look at libusb. 如果你想从用户空间控制USB设备,请查看libusb。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.