簡體   English   中英

Ubuntu第一次編譯內核模塊

[英]Ubuntu compiling kernel module first time

我第一次嘗試編譯一個簡單的內核模塊:

#include <linux/module.h>    
#include <linux/kernel.h>

int init_nodule(void)
{
        printk(KERN_INFO "Hello world1.\n");
        return 0;
}
void cleanup_module(void)
{
        printk(KERN_INFO "Goodbye\n");
}

我使用了obj-m += hello-1.o (這是模塊的名稱),但出現錯誤:

對象-m:未找到命令

為什么會這樣? 我嘗試在網上尋找解決方案,但我發現沒有任何幫助..

編輯:根據@Mathieu 回答修改后,我收到以下錯誤:

> make -C /lib/modules/4.18.0-15-generic/build M=/home/galco modules
make[1]: Entering directory '/usr/src/linux-headers-4.18.0-15-generic'
Makefile:970: "Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel"
scripts/Makefile.build:45: /home/galco/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/galco/Makefile'.  Stop.
Makefile:1534: recipe for target '_module_/home/galco' failed
make[1]: *** [_module_/home/galco] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.18.0-15-generic'
makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

obj-m += hello-1.o必須放在名為Makefile

所以它看起來像:

obj-m += hello-1.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

要啟動構建過程,只需從命令行執行make

更多資源: https : //qnaplus.com/how-to-compile-linux-kernel-module/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM