19

I am trying to compile an example of "hello world" Kernel Module, problems found on ubuntu 11.04, kernel 3.2.6, gcc 4.5.2 and fedora 16, kernel 3.2.7, gcc 4.6.7.

code:

#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");

static int __init hello_init (void)
{
printk("Hello module init\n");
return 0;
}
static void __exit hello_exit (void)
{
printk("Hello module exit\n");
}
module_init(hello_init);
module_exit(hello_exit);

compiled with:

gcc -D__KERNEL__ -I /usr/src/linux/include/ -DMODULE -Wall -O2 -c hello.c -o hello.o

error:

In file included from /usr/src/linux/include/linux/kernel.h:13:0, from /usr/src/linux/include/linux/cache.h:4, from /usr/src/linux/include/linux/time.h:7, from /usr/src/linux/include/linux/stat.h:60, from /usr/src/linux/include/linux/module.h:10, from hello.c:1: /usr/src/linux/include/linux/linkage.h:5:25: fatal error: asm/linkage.h: file not found

then I found in /usr/src/linux/include/ there is no folder named 'asm' but 'asm-generic'; so I made a soft link 'asm' to 'asm-generic', and compiled agail:

this time the error was:

In file included from /usr/src/linux/include/linux/preempt.h:9:0, from /usr/src/linux/include/linux/spinlock.h:50, from /usr/src/linux/include/linux/seqlock.h:29, from /usr/src/linux/include/linux/time.h:8, from /usr/src/linux/include/linux/stat.h:60, from /usr/src/linux/include/linux/module.h:10, from hello.c:1: /usr/src/linux/include/linux/thread_info.h:53:29: fatal error: asm/thread_info.h: file not found

So I realized I was wrong, but why ? T_T

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Raiden Awkward
  • 203
  • 1
  • 2
  • 6

5 Answers5

13
obj-m += hello.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

is a proper way to build modules see kbuild documentation

And to see difference beetween your compiler invocation you could

cat /lib/modules/$(shell uname -r)/build/Makefile

And analyze an output

2r2w
  • 1,384
  • 1
  • 12
  • 29
4
obj-m += hello.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Here hello.c is your kernel source file. just use make to build your hello.ko module.

  • 5
    i know this could work, but why it doesn't work in my way? "gcc -D__KERNEL__ -I /usr/src/linux/include/ -DMODULE -Wall -O2 -c hello.c -o hello.o" – Raiden Awkward Feb 29 '12 at 09:26
1

asm should be a link to the actual architecture you're compiling for, not to asm-generic.
You can't compile a generic kernel module, that would work on a generic architecture. You have to compile it for the particular architecture you're going to use.

I don't know why the asm didn't exist. It should be created as part of the configuration process.
You might get other errors later, if configuration is incomplete in other ways.

ugoren
  • 16,023
  • 3
  • 35
  • 65
  • I've check the kernel src folders(3.2+) on 3 machines with ubuntu, fedora and gentoo, all of them do not contain folder 'asm'. So I think it may not an error.. – Raiden Awkward Feb 29 '12 at 08:17
  • 2
    In the Redhat I checked, `/usr/src/kernels/.../include/asm` is a link to `asm-x86_64`. – ugoren Feb 29 '12 at 10:43
0

The asm includes (such as linkage.h) are architecture specific. There should be a set of directories under:

    /usr/src/kernels/(kernel version goes here)/arch

that provide specific includes for the specific CPU architecture you are targeting your code to be compiled for.

Try adding this to your Makefile:

    KVERSION :=R(shell uname -r)

and add the kernel and architecture (x86 in this example):

    INCDIRS = -I./include -I/usr/src/kernels/$(KVERSION)/include -I/usr/src/kernels/$(KVERSION)/arch/x86
Jay Elston
  • 1,978
  • 1
  • 19
  • 38
-1

module compiling : asm/linkage.h file not found

This means this particular file was not found in specified DIR, which gets specified when we use -I option with make.

We can either link that asm-generic to asm, if all headers are present in asm-generic, or we can use make utility.

Make utility is preferred in case of building kernel modules.

Create a 'Makefile' in working DIR.

obj-m += hello.o
all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Use of -C option will change to DIR specified before reading the makefiles or doing anything else.

So to avoid this error, use -C option with DIR/lib/modules/$(shell uname -r)/build

By this your program will be able to find required files, you will get hello.ko file.

You can add this to kernel modules by

sudo insmod hello.ko

Similarly you can remove by

sudo rmmod hello