Makefile拾遗(1)对-C和M=参数的理解

1.背景

在集成KVM的虚拟化平台上,为实现host主机和guest VM之间数据通信需要创建共享内存,其驱动的添加方式可参考Add device driver (uio_ivshmem.ko) on guest。由于才接触makefile,不懂的地方在此记录,如果理解有误,希望大佬指正。

2.编译生成uio_ivshmem.ko的makefile文件

# obj-m is a list of what kernel modules to build.  The .o and other
# objects will be automatically built from the corresponding .c file -
# no need to list the source files explicitly.

obj-m := uio_ivshmem.o 

# KDIR is the location of the kernel source.  The current standard is
# to link to the associated source tree from the directory containing
# the compiled modules.
KDIR  := /lib/modules/$(shell uname -r)/build

# PWD is the current working directory and the location of our module
# source files.
PWD   := $(shell pwd)

# default is the default make target.  The rule here says to run make
# with a working directory of the directory containing the kernel
# source and compile only the modules in the PWD (local) directory.
default:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

install:
        cp uio_ivshmem.ko /lib/modules/$(shell uname -r)/kernel/drivers/uio/

clean:
        rm -f *.ko *.o uio_ivshmem.mod.c Module.symvers

3.代码解释

(1)命令行敲入make,读取当前目录下的makefile,执行的target为:default

(2)$(MAKE) -C $(KDIR) M=$(PWD) modules 这行代码里,$(MAKE)相当于make,若去掉参数,整行代码可先看做  make modules,说明这个命令用于编译内核模块。

(3)-C参数是选择路径,加上$(KDIR),就会去执行该目录下的makefile,参数是编译外部模块,生成的模块不会直接加载在内核中,M="dir"则是编译的路径。

参考博文:

https://blog.youkuaiyun.com/importantg/article/details/51763551

https://blog.youkuaiyun.com/xiaowulang20082008/article/details/50586985

https://blog.youkuaiyun.com/lgjjeff/article/details/90489331

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值