Linux Device driver Learning essay linux/moudle.h start from scratch file

Source: Internet
Author: User

All programmers get started with the first program from Holle World, Linux embedded driver development is no exception, "Linux device Driver" This book's first program is Hello World. However, the first compilation of life-writing drivers is unusually difficult.

Here is the Hello World program, very simple, basically learned C can be understood, the program consists of two header files, referring to 4 external functions, built up 2 functions.

1#include <linux/init.h>2#include <linux/module.h>3 4Module_license ("Dual BSD/GPL");5 6 Static intHello_init (void)7 {8PRINTK (Kern_alert"Hello world\n");9         return 0;Ten } One  A Static voidHello_exit (void) - { -PRINTK (Kern_alert"Goodbye,cruel world\n"); the } -  - Module_init (hello_init); -Module_exit (Hello_exit);

This program quite clearly pointed out that the driver program structure, the entire driver is loaded by the INIT function, the Exit function to boot the unload. It is worth noting that the driver does not need the main function, which is a more obvious difference between the driver and the normal program.

Note: If you are using an older version of the book or Pdf,hello World code will be different from the above code, it is recommended to use the above code, compared to the old version of the 2.0 era code (the first edition is about 2000 years published), but the content of the book has not changed too much.

Let's get to the point today.

This simple function can be compiled, but if you compile directly with GCC, you will get the following results.

Here mo.c is my source program file.

Can see this time the compiler error, no such file or directory, the book is not to how to solve, can only resort to the almighty degree Niang. Baidu after a circle, I found that the original default compiler does not reference this header file, the address of the head file is under the/usr/src/linux-headers-3.16.0-30, so I use-I to refer to the header file, but can not pass.

After some research, I found that the gods on the Internet were compiled using makefile.

1 obj-m: = mo.o  2  3  Current_dir: =$ (shell pwd)  4  5 C8/>kernel_dir: =/usr/src/linux-headers-$ (Shell uname-R)  6all:  7     $ ( Make)-C $ (kernel_dir) m=$ (current_dir) modules  8  9clean:     RM-RF%.O

These can always be happy to compile the bar, hehe, after make

What exactly does this error message mean? Common sense for 10 hours, I finally found

Mdzz

In short makefile error remember to run with permission ....

Linux Device driver Learning essay linux/moudle.h start from scratch file

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.