Linux 驱动-----module 编译

一、环境

hailei@hailyang-VirtualBox:~/learning/driver$ sudo lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:        16.04
Codename:       xenial
hailei@hailyang-VirtualBox:~/learning/driver$ echo $BASH_VERSION
4.3.48(1)-release

二、C文件

#include 
#include 
#include 

static int __init dev_module_init(void)
{
    printk(KERN_INFO"module init.\r\n");
    return 0;
}

static void __exit _dev_module_exit(void)
{
    printk(KERN_INFO"module exit.\r\n");
}
module_init(dev_module_init);
module_exit(_dev_module_exit);
MODULE_LICENSE("GPL");

三、Makefile


obj-m = test_c_dev.o 
KERNEL = $(shell uname -r)

all: 
	make -C /lib/modules/$(KERNEL)/build M=$(PWD) modules

clean: 
	make -C /lib/modules/$(KERNEL)/build M=$(PWD) clean

四、编译模块

hailei@hailyang-VirtualBox:~/learning/driver$ make all
make -C /lib/modules/4.15.0-38-generic/build M=/home/hailei/learning/driver modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-38-generic'
Makefile:975: "Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel"
  CC [M]  /home/hailei/learning/driver/test_c_dev.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/hailei/learning/driver/test_c_dev.mod.o
  LD [M]  /home/hailei/learning/driver/test_c_dev.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-38-generic'
hailei@hailyang-VirtualBox:~/learning/driver$

五、测试模块

hailei@hailyang-VirtualBox:~/learning/driver$ lsmod 
Module                  Size  Used by
vboxsf                 45056  1
snd_intel8x0           40960  2
snd_ac97_codec        131072  1 snd_intel8x0
ac97_bus               16384  1 snd_ac97_codec
snd_pcm                98304  2 snd_intel8x0,snd_ac97_codec
snd_seq_midi           16384  0
snd_seq_midi_event     16384  1 snd_seq_midi
crct10dif_pclmul       16384  0
snd_rawmidi            32768  1 snd_seq_midi
crc32_pclmul           16384  0
ghash_clmulni_intel    16384  0
pcbc                   16384  0
binfmt_misc            20480  1
joydev                 24576  0
aesni_intel           188416  0
snd_seq                65536  2 snd_seq_midi,snd_seq_midi_event
snd_seq_device         16384  3 snd_seq,snd_seq_midi,snd_rawmidi
snd_timer              32768  2 snd_seq,snd_pcm
aes_x86_64             20480  1 aesni_intel
snd                    81920  11 snd_seq,snd_seq_device,snd_intel8x0,snd_timer,snd_ac97_codec,snd_pcm,snd_rawmidi
input_leds             16384  0
crypto_simd            16384  1 aesni_intel
vboxvideo              36864  2
ttm                   106496  1 vboxvideo
drm_kms_helper        172032  1 vboxvideo
glue_helper            16384  1 aesni_intel
cryptd                 24576  3 crypto_simd,ghash_clmulni_intel,aesni_intel
intel_rapl_perf        16384  0
soundcore              16384  1 snd
serio_raw              16384  0
drm                   401408  5 drm_kms_helper,vboxvideo,ttm
i2c_piix4              24576  0
fb_sys_fops            16384  1 drm_kms_helper
syscopyarea            16384  1 drm_kms_helper
sysfillrect            16384  1 drm_kms_helper
sysimgblt              16384  1 drm_kms_helper
vboxguest             303104  5 vboxsf
mac_hid                16384  0
parport_pc             36864  0
ppdev                  20480  0
lp                     20480  0
parport                49152  3 parport_pc,lp,ppdev
autofs4                40960  2
hid_generic            16384  0
usbhid                 49152  0
hid                   118784  2 usbhid,hid_generic
ahci                   36864  2
psmouse               151552  0
libahci                32768  1 ahci
e1000                 143360  0
pata_acpi              16384  0
video                  45056  0
hailei@hailyang-VirtualBox:~/learning/driver$ insmod test_c_dev.ko
insmod: ERROR: could not insert module test_c_dev.ko: Operation not permitted
hailei@hailyang-VirtualBox:~/learning/driver$ sudo insmod test_c_dev.ko
hailei@hailyang-VirtualBox:~/learning/driver$ lsmod
Module                  Size  Used by
test_c_dev             16384  0
vboxsf                 45056  1
snd_intel8x0           40960  2
snd_ac97_codec        131072  1 snd_intel8x0
ac97_bus               16384  1 snd_ac97_codec
snd_pcm                98304  2 snd_intel8x0,snd_ac97_codec
snd_seq_midi           16384  0
snd_seq_midi_event     16384  1 snd_seq_midi
crct10dif_pclmul       16384  0
snd_rawmidi            32768  1 snd_seq_midi
crc32_pclmul           16384  0
ghash_clmulni_intel    16384  0
pcbc                   16384  0
binfmt_misc            20480  1
joydev                 24576  0
aesni_intel           188416  0
snd_seq                65536  2 snd_seq_midi,snd_seq_midi_event
snd_seq_device         16384  3 snd_seq,snd_seq_midi,snd_rawmidi
snd_timer              32768  2 snd_seq,snd_pcm
aes_x86_64             20480  1 aesni_intel
snd                    81920  11 snd_seq,snd_seq_device,snd_intel8x0,snd_timer,snd_ac97_codec,snd_pcm,snd_rawmidi
input_leds             16384  0
crypto_simd            16384  1 aesni_intel
vboxvideo              36864  2
ttm                   106496  1 vboxvideo
drm_kms_helper        172032  1 vboxvideo
glue_helper            16384  1 aesni_intel
cryptd                 24576  3 crypto_simd,ghash_clmulni_intel,aesni_intel
intel_rapl_perf        16384  0
soundcore              16384  1 snd
serio_raw              16384  0
drm                   401408  5 drm_kms_helper,vboxvideo,ttm
i2c_piix4              24576  0
fb_sys_fops            16384  1 drm_kms_helper
syscopyarea            16384  1 drm_kms_helper
sysfillrect            16384  1 drm_kms_helper
sysimgblt              16384  1 drm_kms_helper
vboxguest             303104  5 vboxsf
mac_hid                16384  0
parport_pc             36864  0
ppdev                  20480  0
lp                     20480  0
parport                49152  3 parport_pc,lp,ppdev
autofs4                40960  2
hid_generic            16384  0
usbhid                 49152  0
hid                   118784  2 usbhid,hid_generic
ahci                   36864  2
psmouse               151552  0
libahci                32768  1 ahci
e1000                 143360  0
pata_acpi              16384  0
video                  45056  0
hailei@hailyang-VirtualBox:~/learning/driver$ 

 

你可能感兴趣的:(Linux)