从源树构建Linux内核模块

时间:2015-07-03 20:36:21

标签: makefile linux-kernel linux-device-driver

我试图为linux-sunxi(Cubieboard 2,A20,Arm Cortex A8)交叉编译Linux内核模块(驱动程序)。我做了什么:

  1. 阅读有关如何为A20构建内核的文章:http://linux-sunxi.org/Linux_Kernel#Compilation。我已经完成了它并且内核已经成功构建。

  2. 尝试使用以下Makefile构建我的简单(只是" Hello,World!")模块:

    ifeq ($(KERNELRELEASE),)
        KERNELDIR = /home/nikita/linux-sunxi/output/lib/modules/3.4.61/build
        PWD =$(shell pwd)
    modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    clean:                    
        rm -rf *.o *.ko .tmp_versions *.mod.c modules.order  Module.symvers 
    else                      
        obj-m :=my_mod.o
    endif
    
  3. 我成功地使用了这样的Makefile来为主机构建内核(KERNELDIR = / lib / modules / $(CURRENT)/ build)。但这是我第一次使用Linux头文件的内核源代码树进行交叉编译。我有以下错误:

    CC [M]  /home/nikita/Kernel_Study/linaro_first_module/my_mod.o
    In file included from /home/nikita/linux-sunxi/arch/x86/include/asm/bitops.h:16:0,
                     from include/linux/bitops.h:22,
                     from include/linux/kernel.h:19,
                     from include/linux/cache.h:4,
                     from include/linux/time.h:7,
                     from include/linux/stat.h:60,
                     from include/linux/module.h:10,
                     from /home/nikita/Kernel_Study/linaro_first_module/my_mod.c:2:
    /home/nikita/linux-sunxi/arch/x86/include/asm/arch_hweight.h: In function ‘__arch_hweight64’:
    /home/nikita/linux-sunxi/arch/x86/include/asm/arch_hweight.h:53:42: error: expected ‘:’ or ‘)’ before ‘POPCNT64’
      asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
                                              ^
    /home/nikita/linux-sunxi/arch/x86/include/asm/alternative.h:93:18: note: in definition of macro ‘ALTERNATIVE’
           "663:\n\t" newinstr "\n664:\n"  /* replacement     */ \
                      ^
    In file included from include/linux/cache.h:5:0,
                     from include/linux/time.h:7,
                     from include/linux/stat.h:60,
                     from include/linux/module.h:10,
                     from /home/nikita/Kernel_Study/linaro_first_module/my_mod.c:2:
    /home/nikita/linux-sunxi/arch/x86/include/asm/processor.h: At top level:
    /home/nikita/linux-sunxi/arch/x86/include/asm/cache.h:7:25: error: ‘CONFIG_X86_L1_CACHE_SHIFT’ undeclared here (not in a function)
     #define L1_CACHE_SHIFT (CONFIG_X86_L1_CACHE_SHIFT)
    

    Linux源代码中的许多其他错误......

    我是Linux内核和设备驱动程序的新手,这就是为什么我觉得,我错过了什么。总之,我做了什么:

    1. 克隆源代码仓库。

    2. 已安装必要的软件包。

    3. 配置了kmake:

      make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- sun7i_defconfig
      
    4. 建造树:

      make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- uImage modules
      
    5. 创建树:

      make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=output modules_install
      
    6. 尝试使用上面的Makefile制作模块。

    7. 另外,我知道没有必要构建一个完整的内核树来制作模块。它已经足以完成Linux标头了。我是对的吗?

      为什么make会转到 / arch / x86 / ?我正在为ARM构建这个......

      我错过了什么?

1 个答案:

答案 0 :(得分:2)

必须指定编译器和我为其构建模块的架构。只需使用以下参数启动make:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-