How to compile a Linux driver module (1)

Source: Internet
Author: User

Write the makefile file directly (assuming the file to be compiled is HELLO.C)

1 Ifneq ($ (kernelrelease),)

2 Obj-m: = hello.o

3 Else

4 Obj-m: = hello.o

5 Kerneldir =/lib/modules/$ (Shell uname-r)/build

6 pwd: = $ (Shell PWD)

7

8 Default:

9 $ (make)-C $ (Kerneldir) m=$ (PWD) modules

Ten endif

The first ifeq ($ (kernelrelease),) is currently useless, and its origin is that the Kernelrelease macros are defined when the kernel is makefile compiled under the Linux source root directory, Then if you start from the source root, make will compile the MYHELLO.O module into the kernel.

Kerneldir =/usr/src/$ (Shell uname-r), this sentence is assigned to the KERNELDIR, this variable is used in the subsequent reference to the kernel source directory used.

PWD: = $ (shell pwd), which is assigned to the PWD variable, is to assign the return result of the $ (shell pwd) to the PWD, which we refer to as the driver where we want to compile the current directory.

$ (make)-C $ (Kerneldir) m=$ (PWD) modules

This is the makefile rule: the $ (make) here is the equivalent of the MAKE,-C option to transfer the current working directory to the location you specify. The function of the "m=" option is that when a user needs to compile an external module based on a kernel, it is necessary to add "M=dir" to the Make Modules command, and the program will automatically find the module source code in the dir directory you specify, compile it, and generate the Ko file.

How to compile a driver module under Linux (1)

Related Article

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.