使用Makefile编译驱动程序时出现编译错误

问题描述 投票:0回答:1

当我尝试将 hello.c 编译为驱动程序组件时,出现如下错误:

root@ubuntu:/home/steven/Downloads# make
make -C /lib/modules/`uname -r`/build M=/home/steven/Downloads modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-29-generic'
  CC [M]  /home/steven/Downloads/hello.o
**cc1: error: code model kernel does not support PIC mode**
scripts/Makefile.build:332: recipe for target '/home/steven/Downloads/hello.o' failed
make[2]: *** [/home/steven/Downloads/hello.o] Error 1
Makefile:1552: recipe for target '_module_/home/steven/Downloads' failed
make[1]: *** [_module_/home/steven/Downloads] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-29-generic'
Makefile:13: recipe for target 'modules' failed
make: *** [modules] Error 2

我的Makefile如下:

ifneq ($(KERNELRELEASE),)
MODULE_NAME = hellomodule
$(MODULE_NAME)-objs := hello.o
obj-m := $(MODULE_NAME).o
else
KERNEL_DIR = /lib/modules/`uname -r`/build
MODULEDIR := $(shell pwd)

.PHONY: modules
default: modules

modules:
    make -C $(KERNEL_DIR) M=$(MODULEDIR) modules

clean distclean:
    rm -f *.o *.mod.c .*.*.cmd *.ko
    rm -rf .tmp_versions
endif

有人可以帮我解决这个问题吗?

makefile linux-kernel driver
1个回答
0
投票

我意识到我的回应可能已经太晚了。然而,就我个人而言,我发现链接的答案在处理现有内核兼容性方面不够充分。

例如,您可能拥有一个内核模块,但无法访问内核的源代码。在这种情况下,我的建议是使用 Docker 等容器解决方案。

您可以通过执行以下命令来完成此操作:

➜构建 sudo docker run -d --name ubuntu_14 -p 8000:22 -it --privileged -v /home:/home -v /boot:/boot -v /lib/modules: /lib/modules -v /usr/src/:/usr/src ubuntu:14.04

在 Docker 容器中,您可以方便地构建模块,消除与旧内核的任何潜在兼容性问题。

  • 请注意,您的答案与 GCC 兼容性有关。
  • 此答案可用于解决构建旧内核时出现的任何其他兼容性问题。
© www.soinside.com 2019 - 2024. All rights reserved.