編寫核心模組hello world

2021-08-31 01:44:54 字數 926 閱讀 5913

1、準備環境

#mkdir ~/src/lkm_example

#cd ~/src/lkm_example

2、建立檔案 lkm_example.c

#include #include #include module_license(「gpl」);

module_author(「robert w. oliver ii」);

module_description(「a ****** example linux module.」);

module_version(「0.01」);

static int __init lkm_example_init(void)

static void __exit lkm_example_exit(void)

module_init(lkm_example_init);

module_exit(lkm_example_exit);

3、編寫makefile

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

4、新增模組測試

#insmod lkm_example.ko

5、檢視:

printk 函式不會輸出在控制台而是輸出在核心日誌。為了看到日誌,我們執行一下命令

# dmesg

6、檢查模組是否被載入

#lsmod | grep 「lkm_example」

7、移除該模組

#rmmod lkm_example

linux 核心模組載入 HelloWorld

1。建立hello目錄 mkdir hello 2。進入hello目錄 cd hello 3。編寫hello.c include include module license gpl static int hello init void static void hello exit void mod...

Linux核心模組初探 HelloWorld

本文通過學習宋寶華老師的 linux裝置驅動開發詳解 第四章而寫的學習筆記,感謝這本好書 因為linux核心架構龐大,元件很多,如果我們把所有需要功能都編譯到linux核心中,就會導致核心很大,並且當我們要在現有核心中新增或刪除功能時都要重新編譯核心。linux使用了模組 module 這一種機制,...

linux核心模組的編寫

之前學了核心模組的編寫,但是沒有動手實驗,這次試驗才發現還是有好動東西需要學習!我的環境是虛擬機器跑centos 7 1.首先編寫模組 include include module license dual bsd gpl static int hello init void static void...