当前位置: 首页 > 工具软件 > ko > 使用案例 >

编译我的hello.ko

桂志新
2023-12-01

一、环境:

1. PC: Fedora13:

[feng@~ #2]$uname -a
Linux Fedora13 2.6.34.8-68.fc13.i686.PAE #1 SMP Thu Feb 17 14:54:10 UTC 2011 i686 i686 i386 GNU/Linux
[feng@~ #3]$

2. 开发板

~ $ uname -a
Linux (none) 2.6.24-rt1-hi3515v100 #7 Thu Nov 10 18:42:18 CST 2011 armv5tejl unknown
~ $ 

二. 编译过程

1. 准备模块相关文件:源文件hello.c及Makefile

[root@c #72]#pwd
/mnt/mdisk5/program/c
[root@c #69]#ls -a
.  ..  hello.c  Makefile
[root@c #70]#cat hello.c 
#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");

static int hello_init(void)
{
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
[root@c #71]#cat Makefile 
ifneq ($(KERNELRELEASE), )
        obj-m := hello.o
else
        #KDIR ?= /mnt/mdisk5/kernel/linux-2.6.34.14
        KDIR ?= /mnt/mdisk5/kernel/linux-2.6.24
        PWD := $(shell pwd)

default:
        $(MAKE) -C $(KDIR) M=$(PWD) modules
        cp hello.ko /tftpboot/DVR/

clean:
        rm -f *.ko *.o *.mod.o *.mod.c Module.symvers modules.order .*.cmd
        rm -rf .tmp_versions

endif
[root@c #72]#

2. 准备内核源码及配置

[feng@~ #52]$cd /mnt/mdisk5/kernel/linux-2.6.24/
[feng@linux-2.6.24 #53]$ls -a
.   arch   COPYING  crypto         drivers  .gitignore   hi_patchset  init  Kbuild  lib                     .mailmap     Makefile  net     REPORTING-BUGS  scripts   sound
..  block  CREDITS  Documentation  fs       .hil_config  include      ipc   kernel  linux_kbuild_export.sh  MAINTAINERS  mm        README  samples         security  usr
[feng@linux-2.6.24 #54]$cp arch/arm/configs/hi3515v100_full_release_defconfig .config
[feng@linux-2.6.24 #55]$ls -a
.   arch   .config  CREDITS  Documentation  fs          .hil_config  include  ipc     kernel  linux_kbuild_export.sh  MAINTAINERS  mm   README          samples  security  usr
..  block  COPYING  crypto   drivers        .gitignore  hi_patchset  init     Kbuild  lib     .mailmap                Makefile     net  REPORTING-BUGS  scripts  sound
[feng@linux-2.6.24 #56]$head -n10 .config 
#
# Automatically generated make config: don't edit
# Linux kernel version: 2.6.24
# Fri Nov 20 09:15:27 2009
#
CONFIG_ARM=y
CONFIG_SYS_SUPPORTS_APM_EMULATION=y
# CONFIG_GENERIC_GPIO is not set
CONFIG_GENERIC_TIME=y
CONFIG_GENERIC_CLOCKEVENTS=y
[feng@linux-2.6.24 #57]$head -n10 Makefile 
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 24
EXTRAVERSION =
NAME = Arr Matey! A Hairy Bilge Rat!

HIL_CONFIG_FILE=.hil_config

# *DOCUMENTATION*
# To see a list of typical targets execute "make help"
[feng@linux-2.6.24 #58]$make prepare
  HOSTCC  scripts/basic/fixdep
scripts/basic/fixdep.c: 在函数‘traps’中:
scripts/basic/fixdep.c:377: 警告:提领类型双关的指针将破坏强重叠规则
scripts/basic/fixdep.c:379: 警告:提领类型双关的指针将破坏强重叠规则
  HOSTCC  scripts/basic/docproc
  HOSTCC  scripts/kconfig/conf.o
  HOSTCC  scripts/kconfig/kxgettext.o
  SHIPPED scripts/kconfig/zconf.tab.c
  SHIPPED scripts/kconfig/lex.zconf.c
  SHIPPED scripts/kconfig/zconf.hash.c
  HOSTCC  scripts/kconfig/zconf.tab.o
scripts/kconfig/lex.zconf.c:1639: 警告:‘input’定义后未使用
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf -s arch/arm/Kconfig
arch/arm/Kconfig:11:warning: 'select' used by config symbol 'ARM' refers to undefined symbol 'HAVE_ARCH_KGDB'
.config:57:warning: trying to assign nonexistent symbol RADIX_TREE_CONCURRENT
.config:58:warning: trying to assign nonexistent symbol HAVE_IMMEDIATE
.config:86:warning: trying to assign nonexistent symbol LTT_TIMESTAMP
.config:87:warning: trying to assign nonexistent symbol HAVE_LTT_UNSTABLE_TSC
.config:88:warning: trying to assign nonexistent symbol HAVE_LTT_CLOCK
.config:89:warning: trying to assign nonexistent symbol HAVE_LTT_SYNTHETIC_TSC
.config:90:warning: trying to assign nonexistent symbol HAVE_LTT_DUMP_TABLES
.config:91:warning: trying to assign nonexistent symbol HAVE_OPROFILE
.config:92:warning: trying to assign nonexistent symbol HAVE_KPROBES
.config:218:warning: trying to assign nonexistent symbol PREEMPT_DESKTOP
.config:219:warning: trying to assign nonexistent symbol PREEMPT_RT
.config:220:warning: trying to assign nonexistent symbol PREEMPT_SOFTIRQS
.config:221:warning: trying to assign nonexistent symbol PREEMPT_HARDIRQS
.config:223:warning: trying to assign nonexistent symbol CLASSIC_RCU
.config:224:warning: trying to assign nonexistent symbol PREEMPT_RCU
.config:225:warning: trying to assign nonexistent symbol RCU_TRACE
.config:869:warning: trying to assign nonexistent symbol RMEM
.config:870:warning: trying to assign nonexistent symbol ALLOC_RTSJ_MEM
.config:1507:warning: trying to assign nonexistent symbol EVENT_TRACE
.config:1508:warning: trying to assign nonexistent symbol WAKEUP_TIMING
.config:1509:warning: trying to assign nonexistent symbol CRITICAL_IRQSOFF_TIMING
.config:1526:warning: trying to assign nonexistent symbol GCOV_PROFILE
.config:1527:warning: trying to assign nonexistent symbol KGDB
*
* Restart config...
*
*
* Kernel Features
*
Tickless System (Dynamic Ticks) (NO_HZ) [N/y/?] n
High Resolution Timer Support (HIGH_RES_TIMERS) [N/y/?] n
Preemption Model
> 1. No Forced Preemption (Server) (PREEMPT_NONE)
  2. Voluntary Kernel Preemption (Desktop) (PREEMPT_VOLUNTARY)
  3. Preemptible Kernel (Low-Latency Desktop) (PREEMPT) (NEW)
choice[1-3]: 
Use the ARM EABI to compile the kernel (AEABI) [N/y/?] n
Memory model
> 1. Flat Memory (FLATMEM_MANUAL)
choice[1]: 1
64 bit Memory and IO resources (EXPERIMENTAL) (RESOURCES_64BIT) [N/y/?] n
*
* USB support
*
USB support (USB_SUPPORT) [Y/n/?] y
  Support for Host-side USB (USB) [Y/n/m/?] y
    USB verbose debug messages (USB_DEBUG) [N/y/?] n
    *
    * Miscellaneous USB options
    *
    USB device filesystem (USB_DEVICEFS) [Y/n/?] y
    USB device class-devices (DEPRECATED) (USB_DEVICE_CLASS) [Y/n/?] y
    Dynamic USB minor allocation (EXPERIMENTAL) (USB_DYNAMIC_MINORS) [N/y/?] n
    *
    * USB Host Controller Drivers
    *
    EHCI HCD (USB 2.0) support (USB_EHCI_HCD) [Y/n/m/?] y
      Full speed ISO transactions (EXPERIMENTAL) (USB_EHCI_SPLIT_ISO) [N/y/?] n
      Root Hub Transaction Translators (EXPERIMENTAL) (USB_EHCI_ROOT_HUB_TT) [N/y/?] n
      Improved Transaction Translator scheduling (EXPERIMENTAL) (USB_EHCI_TT_NEWSCHED) [N/y/?] n
  ISP116X HCD support (USB_ISP116X_HCD) [N/m/y/?] n
  OHCI HCD support (USB_OHCI_HCD) [Y/n/m/?] y
  SL811HS HCD support (USB_SL811_HCD) [N/m/y/?] n
  R8A66597 HCD support (USB_R8A66597_HCD) [N/m/y/?] n
  *
  * USB Device Class drivers
  *
  USB Modem (CDC ACM) support (USB_ACM) [M/n/y/?] m
  USB Printer support (USB_PRINTER) [N/m/y/?] n
  *
  * NOTE: USB_STORAGE enables SCSI, and 'SCSI disk support'
  *
  *
  * may also be needed; see USB_STORAGE Help for more information
  *
  USB Mass Storage support (USB_STORAGE) [M/n/y/?] m
    USB Mass Storage verbose debug (USB_STORAGE_DEBUG) [N/y/?] n
    Datafab Compact Flash Reader support (EXPERIMENTAL) (USB_STORAGE_DATAFAB) [Y/n/?] y
    Freecom USB/ATAPI Bridge support (USB_STORAGE_FREECOM) [Y/n/?] y
    ISD-200 USB/ATA Bridge support (USB_STORAGE_ISD200) [Y/n/?] y
    Microtech/ZiO! CompactFlash/SmartMedia support (USB_STORAGE_DPCM) [Y/n/?] y
    USBAT/USBAT02-based storage support (EXPERIMENTAL) (USB_STORAGE_USBAT) [Y/n/?] y
    SanDisk SDDR-09 (and other SmartMedia) support (EXPERIMENTAL) (USB_STORAGE_SDDR09) [Y/n/?] y
    SanDisk SDDR-55 SmartMedia support (EXPERIMENTAL) (USB_STORAGE_SDDR55) [Y/n/?] y
    Lexar Jumpshot Compact Flash Reader (EXPERIMENTAL) (USB_STORAGE_JUMPSHOT) [Y/n/?] y
    Olympus MAUSB-10/Fuji DPC-R1 support (EXPERIMENTAL) (USB_STORAGE_ALAUDA) [Y/n/?] y
    Support OneTouch Button on Maxtor Hard Drives (EXPERIMENTAL) (USB_STORAGE_ONETOUCH) [N/y/?] n
    Support for Rio Karma music player (USB_STORAGE_KARMA) [N/y/?] n
  The shared table of common (or usual) storage devices (USB_LIBUSUAL) [N/y/?] n
  *
  * USB Imaging devices
  *
  USB Mustek MDC800 Digital Camera support (EXPERIMENTAL) (USB_MDC800) [M/n/y/?] m
  Microtek X6USB scanner support (USB_MICROTEK) [M/n/y/?] m
  USB Monitor (USB_MON) [Y/n/?] y
  *
  * USB port drivers
  *
  *
  * USB Miscellaneous drivers
  *
  EMI 6|2m USB Audio interface support (USB_EMI62) [M/n/y/?] m
  EMI 2|6 USB Audio interface support (USB_EMI26) [M/n/y/?] m
  ADU devices from Ontrak Control Systems (EXPERIMENTAL) (USB_ADUTUX) [N/m/y/?] n
  USB Auerswald ISDN support (EXPERIMENTAL) (USB_AUERSWALD) [M/n/y/?] m
  USB Diamond Rio500 support (EXPERIMENTAL) (USB_RIO500) [M/n/y/?] m
  USB Lego Infrared Tower support (EXPERIMENTAL) (USB_LEGOTOWER) [M/n/y/?] m
  USB LCD driver support (USB_LCD) [M/n/y/?] m
  USB BlackBerry recharge support (USB_BERRY_CHARGE) [N/m/y/?] n
  USB LED driver support (USB_LED) [M/n/y/?] m
  Cypress CY7C63xxx USB driver support (USB_CYPRESS_CY7C63) [N/m/y/?] n
  Cypress USB thermometer driver support (USB_CYTHERM) [N/m/y/?] n
  USB Phidgets drivers (USB_PHIDGET) [N/m/y/?] n
  Siemens ID USB Mouse Fingerprint sensor support (USB_IDMOUSE) [M/n/y/?] m
  Elan PCMCIA CardBus Adapter USB Client (USB_FTDI_ELAN) [N/m/y/?] n
  Apple Cinema Display support (USB_APPLEDISPLAY) [M/n/y/?] m
  USB 2.0 SVGA dongle support (Net2280/SiS315) (USB_SISUSBVGA) [N/m/y/?] (NEW) 
  USB LD driver (USB_LD) [M/n/y/?] m
  PlayStation 2 Trance Vibrator driver support (USB_TRANCEVIBRATOR) [N/m/y/?] n
  IO Warrior driver support (USB_IOWARRIOR) [N/m/y/?] n
  USB testing driver (DEVELOPMENT) (USB_TEST) [M/n/y/?] m
#
# configuration written to .config
#
  CHK     include/linux/version.h
  UPD     include/linux/version.h

  SYMLINK include/asm-arm/arch -> include/asm-arm/arch-hi3515v100
  Generating include/asm-arm/mach-types.h
  CHK     include/linux/utsrelease.h
  UPD     include/linux/utsrelease.h
  SYMLINK include/asm -> include/asm-arm
  CC      arch/arm/kernel/asm-offsets.s
  GEN     include/asm-arm/asm-offsets.h
  CALL    scripts/checksyscalls.sh
<stdin>:1097:2: warning: #warning syscall fadvise64 not implemented
<stdin>:1265:2: warning: #warning syscall migrate_pages not implemented
<stdin>:1321:2: warning: #warning syscall pselect6 not implemented
<stdin>:1325:2: warning: #warning syscall ppoll not implemented
<stdin>:1365:2: warning: #warning syscall epoll_pwait not implemented
[feng@linux-2.6.24 #59]$
[feng@linux-2.6.24 #59]$make scripts
  CC      scripts/mod/empty.o
  HOSTCC  scripts/mod/mk_elfconfig
  MKELF   scripts/mod/elfconfig.h
  HOSTCC  scripts/mod/file2alias.o
  HOSTCC  scripts/mod/modpost.o
  HOSTCC  scripts/mod/sumversion.o
  HOSTLD  scripts/mod/modpost
  HOSTCC  scripts/kallsyms
  HOSTCC  scripts/conmakehash

3. 编译模块,并将模块拷贝到目标目录(/tftpboot/DVR/)

[root@c #72]#pwd
/mnt/mdisk5/program/c
[root@c #73]#cd
[root@~ #74]#cd /mnt/mdisk5/program/c/
[root@c #75]#ls -a
.  ..  hello.c  Makefile
[root@c #76]#make
make -C /mnt/mdisk5/kernel/linux-2.6.24 M=/mnt/mdisk5/program/c modules
make[1]: Entering directory `/mnt/mdisk5/kernel/linux-2.6.24'

  WARNING: Symbol version dump /mnt/mdisk5/kernel/linux-2.6.24/Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /mnt/mdisk5/program/c/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /mnt/mdisk5/program/c/hello.mod.o
  LD [M]  /mnt/mdisk5/program/c/hello.ko
make[1]: Leaving directory `/mnt/mdisk5/kernel/linux-2.6.24'
cp hello.ko /tftpboot/DVR/
[root@c #77]#modinfo hello.ko
filename:       hello.ko
license:        GPL
depends:        
vermagic:       2.6.24-rt1-hi3515v100 mod_unload ARMv5 
[root@c #78]#ls -a
.  ..  hello.c  hello.ko  .hello.ko.cmd  hello.mod.c  hello.mod.o  .hello.mod.o.cmd  hello.o  .hello.o.cmd  Makefile  Module.symvers  .tmp_versions
[root@c #79]#

4. 在开发板上装载和卸载hello.ko

/mnt/nfs $ cd
~ $ pwd
/root
~ $ df
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/mtdblock2           21020     21020         0 100% /
tmpfs                    22400         4     22396   0% /dev
tmpfs                    22400      7920     14480  35% /tmp
tmpfs                    22400         0     22400   0% /mnt
tmpfs                    22400         4     22396   0% /var
/dev/mtdblock1            2048       772      1276  38% /mnt/mtd
192.168.1.37:/tftpboot/DVR  19094240  13034976   5089312  72% /mnt/nfs
~ $ cd /mnt/nfs
/mnt/nfs $ ls
Wireless                  lvr_3515_pf_new           snap_20000101_000143.jpg
hello.ko                  lvrosd                    snap_20000101_000223.jpg
isp_config.ini            res                       snap_20000101_000657.jpg
lvftest                   screenshot_hisi           wiff
/mnt/nfs $ insmod hello.ko 
Hello, world
/mnt/nfs $ lsmod | grep hello    
hello                   1024  0 
/mnt/nfs $ rmmod hello    
Goodbye, cruel world
/mnt/nfs $ lsmod | grep hello
/mnt/nfs $ 

三、说明

1.  我的Fedora13的IP为:192.168.1.37,开发板将Fedora13下的/tftpboot/DVR/ 挂载到 /mnt/nfs/ 下

2.  我上面的运行结果是串口下进行的,所以能看到输出的结果。如果是通过telnet上去的话,可能看不到输出结果的,但是可以用cat /var/log/syslog 或 dmesg 查看

3.  我用的开发板是基于海思Hi3515平台的,所以用的.config来自于arch/arm/configs/hi3515v100_full_release_defconfig文件。

4.  若要对内核源代码进行清理,以便下次重新编译,可以执行"make clean"或"make distclean",这两个的区别可以看内核目录下的Makefile文件,说明很详细!

四、总结

1. 对内核源代码的相关操作:

a)  进入内核源代码目录

b) 生成.config文件,方法有很多,我是运行"cp arch/arm/configs/hi3515v100_full_release_defconfig .config",根据不同的目标环境,复制的源配置文件也不同。

c) 执行: make prepare

d) 执行: make scripts


2. 对自己编写的模块的操作

a) 写好hello.c及Makefile文件(文件名必须为”Makefile", 不能为“makefile")

b) 编译: make


3. 遇到错误,可参考: http://blog.csdn.net/joeblackzqq/article/details/8694431


 类似资料: