11

How do I add an include path for kernel module makefile? I want to include "test_kernel.h" in test_module.c. the "test_kernel.h" resides in other directory "inc" I tried in the following solution in my Makefile but it does not work:

obj-m += test_module.o

test_module:
    $(MAKE) -C "$(LINUX_DIR)" -Iinc $(MAKE_OPTS) modules
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
MOHAMED
  • 41,599
  • 58
  • 163
  • 268

4 Answers4

19

You should make use of EXTRA_CFLAGS in your Makefile. 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.
Hope this helps!

another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
  • 4
    This is very useful. Reading the newer doc, it seems EXTRA_CFLAGS is deprecated now. You may use ccflags-y=-I$(PWD)/inc instead of EXTRA_CFLAGS. Check section 3.7 [here](https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt). – hesham_EE Oct 27 '14 at 17:22
  • EXTRA_CFLAGS also seems to be working. and the path should be specified as seen from the kernel build directory (the one specified with -C). – Chan Kim Jul 21 '21 at 05:48
  • Doc clearly mentions `Flags with the same behaviour were previously named: EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS. They are still supported but their usage is deprecated.` You should use `ccflags-y`. – vmemmap Oct 13 '22 at 07:45
3

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:

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

For a directory "/lib/include" that is two levels up from the source folder.

This is driven from the statement in 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.

Bassem Ramzy
  • 326
  • 3
  • 10
  • yes, I try to use `$(CURDIR)` instead of `$(src)` but its not working.Thank you for the reference link. – goodman May 02 '23 at 11:30
2

are you sure you correctly specified the include in your file?

e.g.:

#include "inc/something.h"

instead of

#include <inc/something.h>
ticcky
  • 688
  • 6
  • 17
1

-I is a GCC flag, not a Make flag.1 You need to pass a variable down to your "sub" Make process; 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. 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.


1. Actually, it is also a Make flag, but for something completely unrelated.
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680