Hello module compilation process for Linux device drivers __linux

Source: Internet
Author: User

Today the Linux device Driver (third edition) of the first module Hello module compiled passed, this thing card for me for a long time, during which I spent a lot of time to see the Linux Program Design (second edition), finally today the mechanical completion of this experiment.

Compilation environment: Virtual machine linux2.6.18 kernel, (if the kernel is not 2.6, you can refer to my kernel upgrade process, another article has a detailed record)

SOURCE program HELLO.C:
////////////////////////////////////////////////////////////////////////////
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

Module_license ("Dual BSD/GPL");
static int hello_init (void)//Some of the definitions above init_modules (void) are not compiled
{
PRINTK (Kern_alert "Hello, world/n");
return 0;
}

static void Hello_exit (void)
{
PRINTK (Kern_alert "Goodbye, world/n");
}

Module_init (Hello_init);
Module_exit (Hello_exit);
////////////////////////////////////////////////////////////////////////

Content of Makefile:


Ifneq ($ (kernelrelease),)
Obj-m: = hello.o
Else
kdir:=/lib/modules/$ (shell uname-r)/build
pwd:=$ (Shell PWD)

All
$ (make)-C $ (Kdir) subdirs=$ (PWD) modules
endif

Clean
Rm-f *.o *.ko *.mod.c. hello*
//////////////////////////////////////////////////////////


Put the hello.c makefile in the same folder Hello, in the Hello directory (my/home/leo/hello) will be prompted Hellomodules folders can not be found, create a hellomodules folder (Home/leo /hellomodules), and then in the Hello Directory (home/leo/hello) compile will prompt hello.c makefile can not find, hello.c makefile copy to Hellomodules directory down, and then compile OK.

Load module:

Insmod./hello.ko
(System hint: insmod command not found)

There are a lot of commands under the Linux virtual machine that can't be found because of path, we can use Whereis command to find, here
Whereis Insmod
(Find Insmod location)
(for example, in/usr/***/insmod, then use:)
/usr/***/insmod./hello.ko
(same changes to the system path can also be done)

After loading with the Lsmod command view, you can see that the Hello module has been loaded into the kernel, rmmod command usage is the same. (Note that rmmod Hello, not Hello.ko) to see the output of the information we can use: demsg | TAIL-N1 can see the output of "Hello World" and Bye

Information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.