linux模組編譯入門__linux

來源:互聯網
上載者:User

1. 在linux系統中找任何一個目錄,建立一個符合模組結構的模組檔案:hello.c

#include <linux/init.h>//module_init()&module_exit相關宏#include <linux/module.h>//所有模組都需要的宏MODULE_LICENSE("GPL");static int hello_init(void){        printk("hello module in\n");        return 0;}static void hello_exit(void){        printk("hello module exit\n");}module_init(hello_init);module_exit(hello_exit);                     


2. 在同一目錄中建立Makefile檔案

obj-m += hello.oKDIR := /lib/modules/$(shell uname -r)/buildPWD :=$(shell pwd)all:        make -C $(KDIR) M=$(PWD)


3. 執行make編譯模組

$ sudo makemake -C /lib/modules/2.6.35-22-generic/build M=/home/linx/Workspace/LLD/hellomodulemake[1]: Entering directory `/usr/src/linux-headers-2.6.35-22-generic'  LD      /home/linx/Workspace/LLD/hellomodule/built-in.o  CC [M]  /home/linx/Workspace/LLD/hellomodule/hello.o  Building modules, stage 2.  MODPOST 1 modules  CC      /home/linx/Workspace/LLD/hellomodule/hello.mod.o  LD [M]  /home/linx/Workspace/LLD/hellomodule/hello.komake[1]: Leaving directory `/usr/src/linux-headers-2.6.35-22-generic'

從控制台資訊上說明已經產生hello.ko了


4. 載入/卸載模組

$ sudo insmod hello.ko //載入模組$ sudo rmmod hello //卸載模組,卸載時不需要加尾碼.ko
$ dmesg | tail -2 //查看最後兩行的調試資訊[29488.207236] hello module in    //說明模組被載入了[29498.831634] hello module exit  //說明模組被卸載了
$ lsmod   //在模組被載入之後用此命令可以查看運行中模組的資訊Module                  Size  Used byhello                    692  0 acpiphp                17207  0 ...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.