使用< linux/types.h>在用户程序中,或< stdint.h>在驱动程序模块代码中...有关系吗? [英] Using <linux/types.h> in user programs, or <stdint.h> in driver module code...does it matter?

查看:490
本文介绍了使用< linux/types.h>在用户程序中,或< stdint.h>在驱动程序模块代码中...有关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发设备驱动程序模块和相关的用户库来处理ioctl()调用.该库将相关的信息放入一个结构中,该结构被传递到驱动程序模块中并在其中解压缩,然后进行处理(我省略了很多步骤,但这是整体思路).

I'm developing a device driver module and associated user libraries to handle the ioctl() calls. The library takes the pertinent info and puts it into a struct, which gets passed into the driver module and unpacked there and then dealt with (I'm omitting a lot of steps, but that's the overall idea).

通过ioctl()通过结构传递的某些数据是uint32_t类型.我发现该类型在<stdint.h><linux/types.h>中定义.到目前为止,我一直在使用<linux/types.h>来定义该值,包括在用户库中.但是我知道在用户空间中使用<linux/*.h>库是一种不好的形式,因此,如果我删除它们并改为使用<stdint.h>,那么当我的驱动程序模块包含struct定义时,它也必须包含<stdint.h>.

Some of the data being passed through the struct via the ioctl() is uint32_t type. I've discovered that that type is defined in <stdint.h> AND <linux/types.h>. So far I've been using <linux/types.h> to define that value, including down in the user libraries. But I understand it is bad form to use <linux/*.h> libraries in user space, so if I remove those and use <stdint.h> instead, then when my driver module includes the struct definition, it will have to be including <stdint.h> also.

在我看来,<linux/types.h>的重点是在内核文件中定义类型,因此我不确定这是否意味着使用<stdint.h>是个坏主意.我还发现,当尝试使用<stdint.h>编译驱动程序模块时,即使重新将<linux/types.h>的所有实例替换为<stdint.h>,也遇到无法重新定义的重新定义的编译错误(并将其放在顶部)包含顺序)

It seems to me that the point of <linux/types.h> is to define types in kernel files, so I'm not sure if that means using <stdint.h> is bad idea there. I also found that when trying to compile my driver module with <stdint.h>, I get compilation errors about redefinitions that won't go away, even if I replace all instances of <linux/types.h> with <stdint.h> (and put it on the top of the include order).

  1. 在用户空间代码中使用linux/*.h包括不是一个好主意吗?
  2. 在内核空间代码中使用<stdint.h>是个坏主意吗?
  3. 如果这两个答案都是肯定的,那么我该如何处理用户库和驱动程序模块共享包含uint32_t的结构的情况?
  1. Is it a bad idea to use linux/*.h includes in user-space code?
  2. Is it a bad idea to use <stdint.h> in kernel-space code?
  3. If the answers to both of those is yes, then how do I handle the situation where a structure containing uint32_t is shared by both the user library and the driver module?

推荐答案

  1. 在用户空间代码中使用linux/*.h包括不是一个好主意吗?

是的,通常.典型的情况是,您应该使用C库头(在本例中为stdint.h和friends),并通过这些用户空间类型与C库进行接口,并让该库处理通过内核与内核进行通信的情况.类型.

Yes, usually. The typical situation is that you should be using the C-library headers (in this case, stdint.h and friends), and interface with the C library though those user-space types, and let the library handle talking with the kernel through kernel types.

尽管您并不处于典型情况.就您而言,您正在编写驱动程序库.因此,您应该使用stdint.h呈现给用户空间的接口,但是当您连接内核驱动程序时使用linux/*.h标头.

You're not in a typical situation though. In your case, you're writing the driver library. So you should be presenting an interface to userspace using stdint.h, but using the linux/*.h headers when you interface to your kernel driver.

因此,在您的情况下,答案是否定的.

So the answer is no, in your case.

  1. 在内核空间代码中使用stdint.h是个坏主意吗?

最肯定是的.

另请参阅: http://lwn.net/Articles/113349/

这篇关于使用&lt; linux/types.h&gt;在用户程序中,或&lt; stdint.h&gt;在驱动程序模块代码中...有关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆