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.csdn.net/importantg/article/details/51763551

https://blog.csdn.net/xiaowulang20082008/article/details/50586985

https://blog.csdn.net/lgjjeff/article/details/90489331

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章