简体   繁体   English

如何使用内核头文件编译用户空间程序

[英]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.

相关问题 如何在程序中包含内核头文件 - How to include kernel headers in a program Linux内核与用户空间程序之间的通信 - Communication between Linux kernel and user space program 如何在用户空间程序和 Linux kernel 模块之间进行通信,以便打印从用户程序发送到 kernel 的消息 - How to make communication between user space program and Linux kernel module in order to print a message sent from user program to the kernel 将内核模块与用户空间程序通信的最佳方式是什么? - What is the best way to communicate a kernel module with a user space program? 如何在内核驱动程序中分配用户空间缓冲区? - How to allocate user space buffer in kernel driver? 从用户空间应用程序调用内核空间中的用户定义函数 - Calling a user defined function in kernel space from user space application program 在编译时将用户空间C函数代码下降/解析到内核空间? - Descend/resolve user-space C function code down to kernel-space at compile time? 如何识别用户空间和内核空间之间的特定套接字? - How to identify a specific socket between User Space and Kernel Space? 用户空间应用程序如何从内核空间接收数据? - how to receive the data by the user space application from the kernel space? 如何从用户空间访问和更改内核空间的变量 - how to acess and change variable of kernel space from user space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM