linux下核心模組編譯初階

來源:互聯網
上載者:User

1、編寫核心模組,以hello為例

#include <linux/init.h>

#include <linux/module.h>

 

MODULE_LICENSE("Dual BSD/GPL"); //核心模組的許可許可權

//模組的載入函數

static int hello_init(void)   

{

printk(KERN_ALERT "hello world!\n");

return 0;

}

//模組的卸載函數

static void hello_exit(void)

{

printk(KERN_ALERT "goodbye hello world!\n");

}

//註冊模組的載入函數

module_init(hello_init);

//註冊模組的卸載函數

module_exit(hello_exit);

 

2、建立Makefile檔案

ifneq ($(KERNELRELEASE), )

         obj-m = hello.o

else

       KERNELDIR ?= /lib/modules/$(shell uname -r )/build

       PWD := $(shell pwd)

default:

        $(MAKE)  -C  $(KERNELDIR)  M=$(PWD)  modules

endif

 

clean:

        rm -f   *.ko  *.o  *.mod   *.mod  *.symvers  *.order

 

3、使用make命令即可以生產核心模組hello.ko,然後使用insmod hello.ko載入核心,使用rmmod hello卸載核心 ,使用lsmod 查看核心中所有模組。

 

注意:

Makefile書寫格式非常嚴格,
all:
<TAB縮排>make -C $(KDIR) M=$(PWD) $(EXTRA_CFLAGS) modules
default:
<TAB縮排>make -C $(KDIR) M=$(PWD)  modules
clean:
<TAB縮排>rm -f *.ko

在拷貝網路代碼的過程中,很可能原有的TAB被若干空格鍵所替代,就會出現Nothing to be done for...的錯誤了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.