Makefile中的-C和M=解析

本文介绍Makefile在嵌入式开发中的使用,重点讲解了-C和M=参数的作用,并通过实例展示了如何利用Makefile进行内核模块的编译。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在进行嵌入式开发过程中,经常需要编写和运行Makefile,且在大型项目开发过程中,一般也都是使用Makefile来进行管理、编译、运行的,所以对Makefile的读写是嵌入式软件工程师必须具备的基本能力。
Makefile中,特别是在内核模块编译过程中最常使用到的两个参数就是“-C”和“M=”。

常用Makefile:
# Makefile
ifneq ($(KERNELRELEASE),)

#kbuild syntax. dependency relationshsip of files and target modules are listed here.

obj-m := helloworld.o  

else

PWD  := $(shell pwd)

KERNEL_VER ?= $(shell uname -r)

KERNEL_DIR := /lib/modules/$(KERNEL_VER)/build
all:

$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules

clean:

rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions *.order *symvers

endif

KERNELRELEASE 是在内核源码的顶层Makefile中定义的一个变量,在第一次读取执行此Makefile时,KERNELRELEASE没有被定义,所以make将读取执行else之后的内容。
当make的目标为all时,-C $(KDIR) 指明跳转到内核源码目录下读取那里的Makefile;M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。
当从内核源码目录返回时,KERNELRELEASE已被被定义,kbuild也被启动去解析kbuild语法的语句,make将继续读取else之前的内容,其为kbuild语法的语句, 指明模块源码中各文件的依赖关系,以及要生成的目标模块名。

Makefile编译运行结果:

shawn@shawn-VirtualBox:~/fly/tmp$ make

make -C /lib/modules/3.16.0-30-generic/build M=/home/shawn/fly/tmp

make[1]: 正在进入目录 `/usr/src/linux-headers-3.16.0-30-generic'

  LD      /home/shawn/fly/tmp/built-in.o

  CC [M]  /home/shawn/fly/tmp/helloworld.o

  Building modules, stage 2.

  MODPOST 1 modules

  CC      /home/shawn/fly/tmp/helloworld.mod.o

  LD [M]  /home/shawn/fly/tmp/helloworld.ko

make[1]:正在离开目录 `/usr/src/linux-headers-3.16.0-30-generic'

shawn@shawn-VirtualBox:~/fly/tmp$ 


root@4a39b502e13d:/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface# make gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.12' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) make -C /nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117 M=/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface modules make[1]: Entering directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' make[2]: Entering directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' CC [M] /nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface/tp_rc.o cc1: error: plugin support is disabled; configure with --enable-plugin cc1: error: plugin support is disabled; configure with --enable-plugin cc1: error: plugin support is disabled; configure with --enable-plugin cc1: error: unrecognized command line option '-Wno-typedef-redefinition' [-Werror] cc1: all warnings being treated as errors scripts/Makefile.build:273: recipe for target '/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface/tp_rc.o' failed make[3]: *** [/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface/tp_rc.o] Error 1 Makefile:2030: recipe for target '/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface' failed make[2]: *** [/nvmp_source_dir/sdk/soc/ssc30x/mpp/ssc308/interface] Error 2 make[2]: Leaving directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' makefile:22: recipe for target 'modules' failed make[1]: *** [modules] Error 2 make[1]: Leaving directory '/nvmp_source_dir/sdk/soc/ssc30x/linux-5.10.117' Makefile:27: recipe for target 'all' failed make: *** [all] Error 2 gcc -v显示--enable-plugin,为什么还会报错呢?
03-13
./build/riscv64-nemu-interpreter: invalid option -- '/' Usage: ./build/riscv64-nemu-interpreter [OPTION...] IMAGE [args] -b,--batch run with batch mode -I,--max-instr max number of instructions executed -l,--log=FILE output log to FILE --small-log=FILE output log to a limited size FILE, but log is always up to date -d,--diff=REF_SO run DiffTest with reference REF_SO -p,--port=PORT run DiffTest with port PORT -D,--statdir=STAT_DIR store simpoint bbv, cpts, and stats in STAT_DIR -w,--workload=WORKLOAD the name of sub_dir of this run in STAT_DIR -C,--config=CONFIG running configuration -c,--restore restoring from CPT FILE -r,--cpt-restorer=R binary of gcpt restorer -S,--simpoint-dir=SIMPOINT_DIR simpoints dir -u,--uniform-cpt uniformly take cpt with fixed interval --cpt-interval=INTERVAL cpt interval: the profiling period for simpoint; the checkpoint interval for uniform cpt --warmup-interval=INTERVAL warmup interval: the warmup interval for SimPoint cpt --cpt-mmode force to take cpt in mmode, which might not work. --manual-oneshot-cpt Manually take one-shot cpt by send signal. --manual-uniform-cpt Manually take uniform cpt by send signal. --checkpoint-format Specify the checkpoint format('gz' or 'zstd'), default: 'gz'. --simpoint-profile simpoint profiling --dont-skip-boot profiling/checkpoint immediately after boot --mem_use_record_file result output file for analyzing the memory use segment -M,--dump-mem=DUMP_FILE dump memory into FILE -R,--dump-reg=DUMP_FILE dump register value into FILE
03-28
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值