So, I've tried following the simple examples to load a "Hello World" device driver found in the O'Rielly Linux Device Drivers manual. The problem is that for some reason it won't work unless I explicitly define the path for my header files in the include statements; i.e. I must type #include </usr/src/kernels/3.19.8-100.fc20.i686+PAE/include/linux/init.h>
instead of just #include </linux/init.h>
here is my makefile
obj-m := hello.o
KDIR =/usr/src/kernels/3.19.8-100.fc20.i686+PAE/include
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
and it returns a can't find error on all header files unless explicitly defined as #include </usr/src/kernels/3.19.8-100.fc20.i686+PAE/include/linux/init.h>
which I'd rather not have to do.
Thanks for your help.
In case it helps anyone, if I keep in the explicit include statements and then compile, the hello.mod.c file that results is as follows.
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
This loads fine using insmod.
Your compiler should have an option to specify the includes' search path. GnuCC (gcc
, g++
) and CLang (clang
, clang++
) both use -I
.
If your header file is at /usr/src/kernels/3.19.8-100/include/linux/init.h
and you invoke, let say g++
with:
g++ -I /usr/src/kernels/3.19.8-100/include/ source.cpp
Then source.cpp
can #include <linux/init.h>
.
Now, you need to learn how to provide that command line option to your compiler through the OS makefiles to build modules. This is documented at several places, but the usual is to define an environment variable CPPFLAGS
containing the additional options:
$ export CPPFLAGS="-I /usr/src/kernels/3.19.8-100/include/"
$ make