module compiling : asm/linkage.h file not found

耗尽温柔 提交于 2019-12-04 02:11:09
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

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.

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.

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!