码迷,mamicode.com
首页 > 系统相关 > 详细

My First Linux Module

时间:2017-07-13 14:24:44      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:linux   center   png   return   text   com   ret   obj   gpl   

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

标签:linux   center   png   return   text   com   ret   obj   gpl   

原文地址:http://www.cnblogs.com/zhanghang-BadCoder/p/7159961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!