Ubuntu12.10 linux kernel module compiled external source - a drive module compiled method substantially

1, first write a simple hello module, hello.c source code as follows:

 1 #ifndef __KERNEL__
 2 #  define __KERNEL__
 3 #endif
 4 #ifndef MODULE
 5 #  define MODULE
 6 #endif
 7 
 8 // 下面的是主要的内容
 9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 
13 MODULE_LICENSE("GPL");
14 
15 static int year=2012;
16 
17 int hello_init()
18 {
19     the printk (KERN_WARNING " the Hello Kernel, IT apos% D \ n-! " , year);
 20 is      return  0 ;
 21 is  }
 22 is  
23 is  
24  void hello_exit ()
 25  {
 26 is      the printk ( " Bye, Kernel \ n-! " );
 27  }
 28  
29  // the following two critical function module 
30  the module_init (hello_init);
 31 is the module_exit (hello_exit);

If the above code does not look familiar, you need to review the following relevant books, such as "Linux Device Drivers, Third Edition", which is famous LDD;

2, old-fashioned method of compiling the driver module:

Write directly to make a makefile rule, the first reference path to the kernel file system, for example as follows:

 1 # The path of kernel source code
 2 INCLUDEDIR = /media/GoldenResources/linux/linux-2.6.30/include
 3 
 4 # Compiler
 5 CC = gcc
 6 
 7 # Options
 8 CFLAGS = -D__KERNEL__ -DMODULE -O -Wall -I$(INCLUDEDIR)
 9 
10 # Target
11 OBJS = hello.o
12 
13 all: $(OBJS)
14 
15 $(OBJS): hello.c
16     $(CC) $(CFLAGS) -c $<
17 
18 install:
19     insmod $(OBJS)
20 
21 uninstall:
22     rmmod hello
23 
24 .PHONY: clean
25 clean:
26     rm -f *.o

Here I am a linux kernel source code path used: /media/GoldenResources/linux/linux-2.6.30/include, pay attention to the correct source path.

Try this compilation:

$make
gcc -D__KERNEL__ -DMODULE -O -Wall -I/media/GoldenResources/linux/linux-2.6.30/include -c hello.c
In file included from /media/GoldenResources/linux/linux-2.6.30/include/linux/kernel.h:11:0,
                 from hello.c:8:
/media/GoldenResources/linux/linux-2.6.30/include/linux/linkage.h:5:25: fatal error: asm/linkage.h: No such file or directory
compilation terminated.
make: *** [hello.o] Error 1

Error: include / linux / linkage.h: 5: 25: fatal error: asm / linkage.h: No such file or directory, the online access to relevant information, to find a good explanation:

Check out: http://stackoverflow.com/questions/9492559/module-compiling-asm-linkage-h-file-not-found 

The main point is this compilation method can not solve specific dependency system, mainly due to historical reasons, linux kernel upgrade soon, more and more complex, it is recommended to use kbuild system to automate; it is a viable use of the following kbuild system to complete.

3, a module compiled using kbuild:

The basic method may Reference:  http://www.mjmwired.net/kernel/Documentation/kbuild/modules.txt

The core idea is, the path through the system kernel system -C indicated by M = specified module source file path and a makefile their configuration, thereby achieving the compilation process.

3.1 Construction of suitable kbuild method makefile:

obj-m := hello.o

all :
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

He began to make:

$ The make 
the make -C / lib / modules / 3.5 of . 0 - . 17 -generic / M = Build / Media / GoldenResources / ARM / Advanced Course the ARM / day core / experiment code / the HelloWorld / Hello modules
 the make [ . 1 ]: Entering `Directory / usr / the src / Linux-headers- 3.5 of . 0 - . 17 -generic '
 scripts / Makefile.build: 44 is : / Media / GoldenResources / ARM / advanced course the ARM / day core / experiment code / HelloWorld / hello / Makefile: No SUCH File or Directory
 the make [ 2 ]: *** No rule to the make target `/ Media / GoldenResources / ARM / ARM advanced courses / kernel day / experiment code / HelloWorld / hello / Makefile' The Stop.. 
The make [ . 1 ]: *** [_module_ / Media / GoldenResources / ARM / Advanced Course the ARM / day core / experiment code / the HelloWorld / Hello] Error 2 
the make [ . 1 ]: Leaving Directory `/ usr / the src / Linux-headers- 3.5 of . 0 - . 17 -generic '
 the make : *** [All] Error 2

Automatically use the currently running kernel, build the corresponding module, but that it can not find the Makefile, while the files in that directory makefile, so try to change the name:

$mv makefile  Makefile

Special attention, to use the Makefile job! When not makefile;

After modification, compile successfully:

$ The make 
the make -C / lib / modules / 3.5 of . 0 - . 17 -generic / M = Build / Media / GoldenResources / ARM / Advanced Course the ARM / day core / experiment code / the HelloWorld / Hello modules
 the make [ . 1 ]: Entering `Directory / usr / the src / Linux-headers- 3.5 of . 0 - . 17 -generic '
   Building modules, Stage 2 . 
  MODPOST . 1 modules # successfully compiled module described
 the make [ . 1 ]: Leaving Directory` / usr / the src / Linux- headers- 3.5 of . 0 - . 17 -generic '

3.2 load and remove kernel modules:

$ Sudo  insmod ./ hello.ko loaded # 
$ sudo rmmod delete the Hello #

I did not see the source code output, the corresponding log to view the system:

$ Tail / var / log / kern.log # ubuntu note the log path 
On Oct 23 is  22 is : 22 is : 22 is qunengrong-Studio- 1450 Kernel: [ 43021.773888 ] Kernel the Hello, IT ' S 2012! 
On Oct 23 is  22 is : 22 is : 37 [ qunengrong -Studio- 1450 Kernel: [ 43037.092339 ] Bye, Kernel!

So far, we have been able to successfully compile and load the kernel modules;

 

4, additional achievements, we should pay attention to matching module and the kernel version:

I suppose direct use another kernel build system, such as 3.5.0-15-generic, but is currently being given to 3.5.0-17-generic kernel, load the operating system, as follows:

$ The make -C / lib / modules / 3.5 of . 0 - 15 -generic / Build M = ` pwd ` modules
 the make : Entering Directory `/ usr / the src / Linux-headers- 3.5 of . 0 - 15 -generic '
   the CC [M] / media / GoldenResources / arm / ARM advanced courses / kernel day / experiment code / the HelloWorld / the Hello / hello.o
 / media / GoldenResources / arm / ARM advanced courses / kernel day / experiment code / HelloWorld / hello / hello .c: 16 : . 5 : warning: function Declaration IS Not the prototype A
 / Media / GoldenResources / ARM / advanced course the ARM / day core / experiment code /HelloWorld/hello/hello.c: 23 is : . 6 : warning:function Declaration IS Not the prototype A 
  Building modules, Stage 2 . 
  MODPOST . 1 modules 
  the CC       / Media / GoldenResources / ARM / Advanced Course the ARM / day core / experiment code / the HelloWorld / Hello / hello.mod.o 
  the LD [M]   / media / GoldenResources / arm / ARM advanced courses / day core / experiment code / the HelloWorld / Hello / hello.ko
 the make : Leaving Directory `/ usr / the src / Linux-headers- 3.5 of . 0 - 15 -generic '
 
$ the sudo  the insmod ./ hello.ko # version inconsistency error
 insmod : error the Inserting ' ./hello.ko ' : -1 Invalid module format

Thus, the use of automatic construction brings convenience, the following general Makefile Share:

obj-m := name.o

all :
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

 

 

Reproduced in: https: //www.cnblogs.com/QuLory/archive/2012/10/23/2736339.html

Guess you like

Origin blog.csdn.net/weixin_33804582/article/details/93154345