加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – Makefile变量替换显然没有完成,即使:=在声明中使用

发布时间:2020-12-13 23:50:22 所属栏目:Linux 来源:网络整理
导读:我有一个主内核模块,其他内核模块与之通信.我已经构建了这样的模块(概念上): main module/ | drivers/ | |driver1 |driver2 driver3 由于这些是内核模块,我需要像这样编译它们: make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
我有一个主内核模块,其他内核模块与之通信.我已经构建了这样的模块(概念上):

main module/
           |
            drivers/
                    |
                    |driver1
                    |driver2
                     driver3

由于这些是内核模块,我需要像这样编译它们:

make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules

但是,由于可以从以前的目录调用驱动程序的Makefile,我需要在调用其他make(linux的make)之前执行$(shell pwd).所以Makefile现在看起来像这样:

CURRENT_DIR := $(shell pwd)

.PHONY: all
all:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURRENT_DIR) modules

到目前为止它很好,它完美地运作.问题是:我有一个驱动程序需要包含的文件,所以我必须给出包含路径.我第一次尝试

EXTRA_CFLAGS += -I../..

并立即明白为什么它不起作用(相对路径将是/ lib / module / …不是当前目录).所以我改成了:

MAIN_MODULE_HOME := $(CURRENT_DIR)/../..
EXTRA_CFLAGS += -I$(MAIN_MODULE_HOME)

奇怪的是,这不起作用!如果我写

EXTRA_CFLAGS += -Ipath/I/get/from/pwd/../..

手动,它编译!谁能解释我做错了什么?在调用make之前,我回显$(CURRENT_DIR)和$(MAIN_MODULE_HOME)并且变量是有意义的.

我知道EXTRA_CFLAGS没有立即评估,但由于CURRENT_DIR和MAIN_MODULE_HOME被声明为:=我不明白事情是如何搞砸的.

(如果有人能更好地说出问题标题,请做!)

解决方法

您应该通过EXTRA_CFLAGS来制作如下:

$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURRENT_DIR) 
           EXTRA_CFLAGS="$(EXTRA_CFLAGS)" modules

更新:

driver1 / Makefile的内容被读取两次:第一次 – 当你在driver1目录中运行make时,第二次 – 由Kbuild系统运行.

首先,CURRENT_DIR:= $(shell pwd)被评估为/home/users/…/main module / drivers / driver1.其次,Kbuild将CURRENT_DIR:= $(shell pwd)评估为/usr/src/linux-headers-2.6.32-33-generic/

这种情况在LDD3,ch2,p24中描述

诀窍是按如下方式编写makefile:

# If KERNELRELEASE is defined,we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
    obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD  := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
#endif

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读