본문으로 바로가기

Basic Module Programming

category Linux/Embedded 2022. 9. 13. 17:39
반응형

Basic Module Programming 실습

Source Code & Makefile 작성

  • 작업 디렉터리 생성(위치는 아무곳이나 상관 없음)
$ mkdir module
$ cd module
$ mkdir hellomodule

  • Source Code 작성
    • 생성한 hellomodule 디렉터리 밑에 생성(hellomodule.c)
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

MODULE_LICENSE("GPL")

int module_start(void)
{
    printk("Module init\n");
    printk("Hello, Linux Module ...\n");
    return 0;
}

int module_end(void)
{
    printk("Module Clean Up\n");
}

module_init(module_start);            // module programming은 따로 main이 없이 정해진 형식에 맞춰 작성,
module_exit(module_end);            // init, exit는 필수 항목이고 작성한 함수를 인자로 추가

  • Makefile 작성
    • 생성한 hellomodule 디렉터리 밑에 생성(Makefile)
obj-m += hellomodule.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all :
        make -C $(KDIR) M=$(PWD) modules
clean :
        rm -rf hellomodule.o hellomodule.mod.* hellomodule.ko

빌드 및 결과 확인

  • 빌드
$ cd module/hellomodule
$ make
$ ls

  • module 설치 및 결과 확인
$ insmod hellomodule.ko
$ lsmod | head -n 5
$ dmsg | tail -n 5
$ rmmod hellomodule
$ dmesg | tail -n 5

반응형

'Linux > Embedded' 카테고리의 다른 글

Character Device Driver 실습  (0) 2022.09.15
Arg(인자처리) Module Programming  (0) 2022.09.15
Basic System Call 구현  (0) 2022.09.07