我正在尝试升级我的守护进程代码,以便它不是可执行文件,而是内核模块,但即使使用一个简单的例子我也很困难。
这是我尝试过的代码:
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void){
printk("Hello world 1.\n");
return 0;
}
void cleanup_module(void){
printk(KERN_ALERT "Goodbye world 1.\n");
}
因为我知道我不能直接编译代码,所以我尝试复制并稍微修改makefile,如下所示:
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc
OBJS := ${patsubst %.c, %.o, ${wildcard *.c}}
all: ${OBJS}
然后在命令行上我执行了“make”,期望安装程序:
这是我的输出:
gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include -c -o d.o d.c
In file included from /lib/modules/2.6.21.5-smp/build/include/asm/thread_info.h:16,
from /lib/modules/2.6.21.5-smp/build/include/linux/thread_info.h:21,
from /lib/modules/2.6.21.5-smp/build/include/linux/preempt.h:9,
from /lib/modules/2.6.21.5-smp/build/include/linux/spinlock.h:49,
from /lib/modules/2.6.21.5-smp/build/include/linux/module.h:9,
from d.c:1:
/lib/modules/2.6.21.5-smp/build/include/asm/processor.h:82: error: 'CONFIG_X86_L1_CACHE_SHIFT' undeclared here (not in a function)
/lib/modules/2.6.21.5-smp/build/include/asm/processor.h:82: error: requested alignment is not a constant
In file included from /lib/modules/2.6.21.5-smp/build/include/linux/sched.h:51,
from /lib/modules/2.6.21.5-smp/build/include/linux/utsname.h:35,
from /lib/modules/2.6.21.5-smp/build/include/asm/elf.h:12,
from /lib/modules/2.6.21.5-smp/build/include/linux/elf.h:7,
from /lib/modules/2.6.21.5-smp/build/include/linux/module.h:15,
from d.c:1:
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:33:3: error: #error You lose.
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
/lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if
In file included from /lib/modules/2.6.21.5-smp/build/include/linux/module.h:21,
from d.c:1:
/lib/modules/2.6.21.5-smp/build/include/asm/module.h:62:2: error: #error unknown processor family
make: *** [d.o] Error 1
当我只处理两个记录良好的函数时,为什么要获取所有这些错误?我的makefile关闭了吗?请指教?
答案 0 :(得分:1)
没关系。我发现自己是一个答案:
https://www.kernel.org/doc/Documentation/kbuild/modules.txt
它声明:
To build against the running kernel use:
$ make -C /lib/modules/`uname -r`/build M=$PWD
Then to install the module(s) just built, add the target
"modules_install" to the command:
$ make -C /lib/modules/`uname -r`/build M=$PWD modules_install