Helloworld in the Linux device driver is compiled successfully!

Source: Internet
Author: User
Helloworld in the Linux device driver is compiled successfully!


I just got started with the development of the Linux Device Driver. After a few days, I finally compiled the helloworld demo! Tricky !!!! Go to the topic!

The following describes how to build an environment and compile helloworld in fc7:
1, we need to download fc7 kernel source code RPM package, CAT/proc/version, if show Linux version 2.6.21-1.3194.fc7.. we need to download the kernel-2.6.21-1.3194.fc7.src.rpm package, remember it is SRC package, do not make a mistake!
2. After downloading the RPM package, execute the command: rpm-uvh kernel-2.6.21-1.3194.fc7.src.rpm
Note: This command writes RPM content to the path
/Usr/src/RedHat/sourse and/usr/src/RedHat/specs
3. CD/usr/src/RedHat/specs
4. rpmbuild-BP -- target = i386 kernel-2.6.spec
Note: This command will put the kernel source code tree in the directory/usr/src/RedHat/build/kernel-2.6.21/linux-2.6.21.i386

5. CD/usr/src/RedHat/build/kernel-2.6.21/linux-2.6.21.i386
6. Make menuconfig
Make sure you have selected the following features:
Loadable module support --->
[*] Enable loadable module support
[*] Module unloading
[] Module Versioning support (experimental)
[*] Automatic kernel module Loading

7. slightly change the makefile:
Each kernel name contains its version number, which is also the value displayed by the uname-R command. The first four lines of the kernel makefile define the kernel name. To protect the official kernel from being damaged, makefile has been modified to generate a name different from that of the running kernel. Before inserting a running kernel into a module, this module must compile the running kernel. To do this, you must edit
Makefile.
For example, if uname-r returns the 2.6.21-1.3194_fc7 string, the extraversion definition is defined from:
Extraversion =-prep
To:
Extraversion =-1.3194 _ fc7

8. Make bzimage to compile the kernel
9. Make modules compilation Module
Note: Make modules_install is required in many places on the Internet, but this is not the case! We only need to set up the environment and do not need to install modules.
10. ln-S/usr/src/RedHat/build/kernel-2.6.21/linux-2.6.21.i386/usr/src/kernels/2.6.21-1.3194.fc7-i686
Note: You can use LS-L/lib/modules/$ (uname-R)/build to browse. This link is used to connect to the/usr/src/kernels/2.6.21-1.3194.fc7-i686 directory, therefore, such a directory must be generated.

11. Compile the kernel module source file: // hello. c
# Include <Linux/init. h>
# Include <Linux/module. h>
Module_license ("dual BSD/GPL ");

Static int hello_init (void ){
Printk (kern_alert "Hello World ");
Return 0;
}

Static void hello_exit (void ){
Printk (kern_alert "goodbye ");
}

Module_init (hello_init );
Module_exit (hello_exit );

12. Compile makefile: # makefile
OBJ-M: = Hello. o
Kdir: =/lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Default:
$ (Make)-C $ (kdir) M = $ (PWD) Modules

Note: you must use a tab space before $ (make). Otherwise, nothing to do may occur!

13. Execute the make command to compile the file. After the command is executed, several files will be generated:
Hello. Ko
Hello. Mod. c
Hello. Mod. o
Hello. o

14. Run the following command:
# Insmod hello. Ko
The returned MESSAGE Hello world is displayed.
Then run the following command:
# Rmmod hello
The returned information is goodbye.
If not, it is output to the system log file. You can view the file:/var/log/messages.

Problem Analysis:
1. "invalid module format" appears during insmod"
Answer: The compilation targets are different. For example, i686 is compiled and i386 is installed. Let's review step 4 -- target = i386, therefore, if this error occurs, select the correct compilation target and re-compile it!
2. Make: Nothing to be done for 'default' is displayed during make '.
Answer: See the note in step 12!

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.