My First Linux Module

Posted ++C+C++

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了My First Linux Module相关的知识,希望对你有一定的参考价值。

My First Linux Module

Today, I successfully build my first linux hello module.

First of all add a directory named hello in the kernel/driver, and add a file hello.c, write codes like bellow:

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

static int __init hello_init(void)
{
    printk(KERN_ERR " Hello, world!\\n");
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_ERR " Goodbye, world!\\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("Bob, Zhang");
MODULE_LICENSE("Dual BSD/GPL");

MODULE_DESCRIPTION("A simple hello world demo");
MODULE_ALIAS("A simple module");

Then create a Kconfig file:

config HELLO
    tristate "HELLO WORLD Driver!"
    default m
    help
        HELLO WORLD

And create a Makefile file:

obj-m += hello.o

Next Add the Kconfig and Makefile into the Kconfig file and Makefile file in parent directory.

Finally run the commands bellow:

make ARCH=arm CROSS_COMPILE=$tool_prefix my_kernel_defconfig
make ARCH=arm CROSS_COMPILE=$tool_prefix modules
mkdir ./moduls_temp
make ARCH=arm CROSS_COMPILE=$tool_prefix modules_install INSTALL_MOD_PATH=./modules_temp

 

At last, the demo run like this:

 

以上是关于My First Linux Module的主要内容,如果未能解决你的问题,请参考以下文章

my first bash coding

my first blog by cnblogs

Python--my first try!

My First Spring-Boot Project

HDU 3706 Second My Problem First (单调队列)

My first blog: GDT与基本描述符的结构