一、问题描述
在写好驱动程序要编译的时候,出现以下错误提示(中间也有很多类似头文件和定义缺失的提示,篇幅问题没有放上来):
make -C /home/lzh/study_1/alientek-alpha/kernel-alientek M=/home/lzh/study_1/study_drivers/1_chardevbase modules
make[1]: Entering directory '/home/lzh/study_1/alientek-alpha/kernel-alientek'
CC [M] /home/lzh/study_1/study_drivers/1_chardevbase/chardevbase.o
In file included from ./arch/x86/include/asm/bitops.h:16:0,
from include/linux/bitops.h:36,
from include/linux/kernel.h:10,
from /home/lzh/study_1/study_drivers/1_chardevbase/chardevbase.c:2:
./arch/x86/include/asm/arch_hweight.h: In function ‘__arch_hweight64’:
./arch/x86/include/asm/arch_hweight.h:53:42: error: expected ‘:’ or ‘)’ before ‘POPCNT64’
asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
^
./arch/x86/include/asm/alternative.h:125:28: note: in definition of macro ‘ALTINSTR_REPLACEMENT’
b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
^
./arch/x86/include/asm/arch_hweight.h:53:7: note: in expansion of macro ‘ALTERNATIVE’
asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
^
In file included from ./arch/x86/include/asm/pgtable_types.h:250:0,
from ./arch/x86/include/asm/processor.h:18,
from ./arch/x86/include/asm/thread_info.h:49,
from include/linux/thread_info.h:54,
from ./arch/x86/include/asm/preempt.h:6,
from include/linux/preempt.h:18,
from include/linux/spinlock.h:50,
from include/linux/seqlock.h:35,
from include/linux/time.h:5,
from include/uapi/linux/timex.h:56,
from include/linux/timex.h:56,
from include/linux/sched.h:19,
from include/linux/blkdev.h:4,
from include/linux/ide.h:12,
from /home/lzh/study_1/study_drivers/1_chardevbase/chardevbase.c:4:
include/asm-generic/pgtable-nopud.h: At top level:
include/asm-generic/pgtable-nopud.h:15:0: warning: "PUD_SHIFT" redefined
#define PUD_SHIFT PGDIR_SHIFT
^
In file included from ./arch/x86/include/asm/pgtable_types.h:205:0,
from ./arch/x86/include/asm/processor.h:18,
from ./arch/x86/include/asm/thread_info.h:49,
from include/linux/thread_info.h:54,
from ./arch/x86/include/asm/preempt.h:6,
from include/linux/preempt.h:18,
from include/linux/spinlock.h:50,
from include/linux/seqlock.h:35,
from include/linux/time.h:5,
from include/uapi/linux/timex.h:56,
from include/linux/timex.h:56,
from include/linux/sched.h:19,
from include/linux/blkdev.h:4,
from include/linux/ide.h:12,
from /home/lzh/study_1/study_drivers/1_chardevbase/chardevbase.c:4:
./arch/x86/include/asm/pgtable_64_types.h:33:0: note: this is the location of the previous definition
#define PUD_SHIFT 30
^
^
In file included from include/linux/sched.h:32:0,
from include/linux/blkdev.h:4,
from include/linux/ide.h:12,
from /home/lzh/study_1/study_drivers/1_chardevbase/chardevbase.c:4:
include/linux/cputime.h:4:25: fatal error: asm/cputime.h: 没有那个文件或目录
compilation terminated.
scripts/Makefile.build:264: recipe for target '/home/lzh/study_1/study_drivers/1_chardevbase/chardevbase.o' failed
make[2]: *** [/home/lzh/study_1/study_drivers/1_chardevbase/chardevbase.o] Error 1
Makefile:1384: recipe for target '_module_/home/lzh/study_1/study_drivers/1_chardevbase' failed
make[1]: *** [_module_/home/lzh/study_1/study_drivers/1_chardevbase] Error 2
make[1]: Leaving directory '/home/lzh/study_1/alientek-alpha/kernel-alientek'
Makefile:9: recipe for target 'kernel_modules' failed
make: *** [kernel_modules] Error 2
lzh@ubuntu16:~/study_1/study_drivers/1_chardevbase$ make
make -C /home/lzh/study_1/alientek-alpha/kernel-alientek M=/home/lzh/study_1/study_drivers/1_chardevbase modules
make[1]: Entering directory '/home/lzh/study_1/alientek-alpha/kernel-alientek'
Makefile:610: arch//Makefile: 没有那个文件或目录
make[1]: *** No rule to make target 'arch//Makefile'。 停止。
make[1]: Leaving directory '/home/lzh/study_1/alientek-alpha/kernel-alientek'
Makefile:9: recipe for target 'kernel_modules' failed
make: *** [kernel_modules] Error 2
但是我的Makefile文件十分简单,应该没有什么问题:
KERNELDIR :=/home/lzh/study_1/alientek-alpha/kernel-alientek
CURRENT_PATH := $(shell pwd)
ENV:=ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
obj-m :=chardevbase.o
build :kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
二、问题解决
找到用于编译驱动模块的linux源码,进入它的Makefile文件,进行如下修改:
在大概255行的地方:
将:
ARCH ?= $(SUBARCH)
CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)
改为:
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-gnueabihf-
注意:
1、这边重新定义的值是没有括号的了。
2、由于我用的是arm-linux-gnueabihf这个交叉编译器,所以这里修改为它,使用其他编译器的话记得进行对应修改。