从多个源文件构建内核模块

时间:2012-09-05 19:16:06

标签: c compiler-construction makefile kernel-module

我遇到了我存在的苦难,Makefile。我正在尝试创建一个名为“can.ko”的内核模块(.ko文件)。我正在编写十几个源文件:

TARGET = can
OBJS = can_core.o can_open.o can_select.o can_sysctl.o can_write.o can_close.o \
       can_ioctl.o can_read.o can_util.o can_debug.o can_error.o can_async.o
KDIR = /lib/modules/3.1.10-1.16-desktop/build
PWD := $(shell pwd)

obj-m += $(TARGET).o

default:
make -C $(KDIR) M=$(PWD) modules
$(TARGET).o : $(OBJS)

编译器吐出无法找到的内容。 “can.c”。

make[4]: *** No rule to make target `/home/mike/iCOM-SDKv1.11.000/framework/drivers/can/can.c', needed by `/home/mike/iCOM-SDKv1.11.000/framework/drivers/can/can.o'.  Stop.

我很确定我不需要在源文件之后命名输出对象,所以显然有些错误。我做了一些关于将多个源文件放在一起并且发现这是另一种选择的方法:

TARGET = can
can-objs := can_core.c can_open.c can_select.c can_sysctl.c can_write.c \
   can_close.c can_ioctl.c can_read.c can_util.c \
   can_debug.c can_error.c \
   can_async.c
KDIR = /lib/modules/3.1.10-1.16-desktop/build
PWD := $(shell pwd)

obj-m += $(TARGET).o

default:
make -C $(KDIR) M=$(PWD) modules

这次我被告知它不知道如何处理.c文件:

LD [M]  /home/mike/iCOM-SDKv1.11.000/framework/drivers/can/can.o
/home/mike/iCOM-SDKv1.11.000/framework/drivers/can/can_core.c: file not recognized: File format not recognized

任何人都可以帮我把这个东西搞定吗?

1 个答案:

答案 0 :(得分:5)

can-objs需要引用.o文件,而不是.c文件。

can-objs := can_core.o can_open.o can_select.o can_sysctl.o can_write.o \
    can_close.o can_ioctl.o can_read.o can_util.o \
    can_debug.o can_error.o \
    can_async.o