Compile the General makefile of the kernel driver

Source: Internet
Author: User
Generally, you can use a general makefile when writing the kernel driver, saving you from the trouble of writing your own. The following is a file I used: ifneq ($ (kernelrelease ),)
# Call from kernel build system
Module-objs: = Hello. o
OBJ-M: = Hello. o

Else

Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules
Endif

Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

Module-objs: = This is followed by a general. o file collection. How many. c files are there? How many. O files are there? For example:

File1.c and file2.c
The statement is as follows:
Module-objs: = file1.o file2.o
Finally, the module in Module-objs should be replaced by the name of the. o file in obj-M.
Hello-objs: = Hello. o
OBJ-M: = Hello. o
If there are multiple files in Module-objs: = file1.o file2.o, do not duplicate the. o file in obj-M: = Hello. O with the name in Module-objs.
Let's take a look at two examples. file C, hello. C and hello1.c are as follows: /* ===================================================== ======================================
A simple kernel module: "Hello World"
========================================================== ===================================== */

/*
* Hello. c
*/
# Include <Linux/init. h>
# Include <Linux/module. h>

Module_license ("GPL ");

Static int hello_init (void)
{
Printk (kern_alert "Hello world in, ztz0223 ");
Return 0;
}

Module_init (hello_init );
Module_author ("ztz0223 ");
Module_description ("a simple hello World module ");
Module_alias ("a simplest module ");

// Hello1.c
# Include <Linux/init. h>
# Include <Linux/module. h>
Static void hello_exit (void)
{
Printk (kern_alert "Hello World quit, ztz0223! ");
}
Module_exit (hello_exit );

Makefile:
Ifneq ($ (kernelrelease ),)
# Call from kernel build system
My_hello-objs: = Hello. O hello1.o
OBJ-M: = my_hello.o
Else
Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules
Endif
Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

The final kernel module is my_hello.ko, which can be loaded and uninstalled.
Another point is that the makefile above should not be copied directly from above, but should be manually written as above.

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.