1

Im a windows driver programmer who is a complete newbie in linux kernel development.I have installed linux kernel headers. I am trying my helloworld module in linux kernel.

#include <linux/init.h>
#include <linux/module.h>
/*MODULE_LICENSE("Dual BSD/GPL");*/
static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);

following is the code for my module. makefile for my build is

obj-m +=tryout.o

KDIR =/usr/src/linux-headers-4.13.0-37-generic

all:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
    rm -rf *.o *.ko *.mod.* *.symvers *.order

but im getting 'fatal error: linux/init.h: No such file or directory while making this module'. What could be the possible reason? and how can i resolve it?

6
  • Exactly what it says: there is no such a file in the directories it searches for the include files. No more no less. Commented Apr 8, 2018 at 8:59
  • I have found that KDIR =/usr/src/linux-headers-4.13.0-37-generic/include/linux contains the header im using and it exists. Commented Apr 8, 2018 at 9:12
  • And? Compiler needs to know it. you have gcc -I option or modify the env. variable. Commented Apr 8, 2018 at 11:23
  • Exact error message, please. @PeterJ_01: It is the Linux kernel build system which should adjust include paths for kernel headers. One shouldn't do that manually. Commented Apr 8, 2018 at 13:25
  • @Tsyvarev should does not mean will. Commented Apr 8, 2018 at 21:31

1 Answer 1

1

Your Makefile is mis-configured. In particular you used SUBDIRS whereas you're supposed to use M and your $(PWD) is meaningless, you should use pwd to be simple (or $$PWD); Here's how you should set it up:

    ifneq ($(KERNELRELEASE),)
    # kbuild part of makefile
    obj-m  := tryout.o
    # any other c files that you would like to include go into 
    # yourmodule-y := <here> e.g.:

    # tryout-y := tryout-1.o tryout-2.o 

    else
    # normal makefile
    KDIR ?= /usr/src/linux-headers-4.13.0-37-generic

    # you really should set KDIR up as:
    # KDIR := /lib/modules/`uname -r`/build

    all::
        $(MAKE) -C $(KDIR) M=`pwd` $@

    # Any module specific targets go under here
    # 

    endif

Configuring your makefile like this will allow you to simply type make in your module directory and it will invoke the Kernel's kbuild subsystem which will in-turn use the kbuild part of your Makefile.

Read up on https://www.kernel.org/doc/Documentation/kbuild/modules.txt for all the different permutations on how to do this. It comes with examples.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.