简体   繁体   中英

How to build a single source file of a kernel module

I want to compile a .c file into an .o file so that in a separate later stage I could link it with others to produce a loadable module ( .ko file).

I tried to follow Kbuilds documentation (2.4 here ), but had no success:

obj-m: myfile.o

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

The output is:

 $ make
cc    -c -o myfile.o myfile.c
myfile.c:42:26: fatal error: linux/printk.h: No such file or         directory
 #include <linux/printk.h>
                          ^
compilation terminated.
<builtin>: recipe for target 'myfile.o' failed
make: *** [myfile.o] Error 1

First thing, as you are getting fatal here regarding headers, so first include all required header file

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */

Second thing, in Makefile target should be modules instead of single .o file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM