简体   繁体   English

如何为 kernel 模块 makefile 添加包含路径

[英]How do I add an include path for kernel module makefile

How do I add an include path for kernel module makefile?如何为 kernel 模块 makefile 添加包含路径? I want to include "test_kernel.h" in test_module.c.我想在 test_module.c 中包含“test_kernel.h”。 the "test_kernel.h" resides in other directory "inc" I tried in the following solution in my Makefile but it does not work: “test_kernel.h”位于其他目录“inc”中 我在 Makefile 中尝试了以下解决方案,但它不起作用:

obj-m += test_module.o

test_module:
    $(MAKE) -C "$(LINUX_DIR)" -Iinc $(MAKE_OPTS) modules

You should make use of EXTRA_CFLAGS in your Makefile . 您应该在Makefile使用EXTRA_CFLAGS Try something on these lines: 尝试这些方面的东西:

obj-m += test_module.o
EXTRA_CFLAGS=-I$(PWD)/inc

test_module:
    $(MAKE) -C "$(LINUX_DIR)" $(MAKE_OPTS) modules

See section 3.7 Compilation Flags section here . 请参见此处的 3.7 Compilation Flags部分。
Hope this helps! 希望这可以帮助!

are you sure you correctly specified the include in your file? 你确定你在文件中正确指定了包含吗?

eg: 例如:

#include "inc/something.h"

instead of 代替

#include <inc/something.h>

For me, many trials have failed, until one of them has succeeded.对我来说,许多试验都失败了,直到其中一个成功。 Using $(src) in the path will do it with ccflags-y , for instance:在路径中使用$(src)将使用ccflags-y来完成,例如:

# Include Paths
ccflags-y       += -I$(src)/../../lib/include 

For a directory "/lib/include" that is two levels up from the source folder.对于比源文件夹高两级的目录“/lib/include”。

This is driven from the statement in Kernel.org这是由Kernel.org中的声明驱动的

Always use $(src) when referring to files located in the src tree Specially, if your source code is in directory that is outside the Linux Kernel Tree.引用位于 src 树中的文件时始终使用 $(src) 特别是,如果您的源代码位于 Linux Kernel 树之外的目录中。

-I is a GCC flag, not a Make flag. -I是GCC标志,而不是Make标志。 1 You need to pass a variable down to your "sub" Make process; 1您需要将变量传递给您的“子”Make过程; perhaps something like this: 也许是这样的:

$(MAKE) -C "$(LINUX_DIR)" CPPFLAGS="-Iinc" $(MAKE_OPTS) modules

where CPPFLAGS is a standard Make variable that's used in the implicit rules. 其中CPPFLAGS是隐式规则中使用的标准Make变量。 Feel free to use your own variable instead, and ensure it's used appropriately in the sub-make. 您可以随意使用自己的变量,并确保在子制作中使用它。

The Make manual gives more details on communicating variables between Make instances: http://www.gnu.org/software/make/manual/make.html#Variables_002fRecursion . Make手册提供了有关在Make实例之间传递变量的更多详细信息: http//www.gnu.org/software/make/manual/make.html#Variables_002fRecursion


1. Actually, it is also a Make flag, but for something completely unrelated. 实际上,它也是一个Make旗帜,但是对于一些完全不相关的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM