Makefile中的-C和M=解析

本文介绍Makefile在嵌入式开发中的作用及如何使用-C和M=参数。通过示例说明了如何配置Makefile来编译内核模块,并解释了这些配置如何帮助管理和构建大型项目。

转载地址:http://blog.youkuaiyun.com/xiaowulang20082008/article/details/50586985


在进行嵌入式开发过程中,经常需要编写和运行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
"D:\Microchip\xc8\v2.40\bin\xc8-cc.exe" -mcpu=16F18854 -Wl,-Map=dist/default/production/NTC_20Channel.X.production.map -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -mdfp="D:/Microchip/MPLABX/v6.00/packs/Microchip/PIC16F1xxxx_DFP/1.9.163/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -Wl,--memorysummary,dist/default/production/memoryfile.xml -o dist/default/production/NTC_20Channel.X.production.elf build/default/production/main.p1 main.c:25:: error: (1363) unknown configuration setting/register (BBSIZE = BB512) used main.c:26:: error: (1363) unknown configuration setting/register (BBEN = OFF) used main.c:27:: error: (1363) unknown configuration setting/register (SAFEN = OFF) used main.c:28:: error: (1363) unknown configuration setting/register (WRTAPP = OFF) used main.c:29:: error: (1363) unknown configuration setting/register (WRTB = OFF) used main.c:30:: error: (1363) unknown configuration setting/register (WRTC = OFF) used main.c:31:: error: (1363) unknown configuration setting/register (WRTSAF = OFF) used (908) exit status = 1 nbproject/Makefile-default.mk:138: recipe for target 'dist/default/production/NTC_20Channel.X.production.hex' failed make[2]: Leaving directory 'C:/Users/MJY/Desktop/MPLAB/NTC_20Channel.X' nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed make[1]: Leaving directory 'C:/Users/MJY/Desktop/MPLAB/NTC_20Channel.X' nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
最新发布
11-05
Using built-in specs. COLLECT_GCC=arm-none-eabi-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-none-eabi/10.3.1/lto-wrapper Target: arm-none-eabi Configured with: ../configure --build=x86_64-linux-gnu --prefix=/usr --includedir='/usr/lib/include' --mandir='/usr/lib/share/man' --infodir='/usr/lib/share/info' --sysconfdir=/etc --localstatedir=/var --disable-option-checking --disable-silent-rules --libdir='/usr/lib/lib/x86_64-linux-gnu' --libexecdir='/usr/lib/lib/x86_64-linux-gnu' --disable-maintainer-mode --disable-dependency-tracking --mandir=/usr/share/man --enable-languages=c,c++,lto --enable-multilib --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --enable-tls --build=x86_64-linux-gnu --target=arm-none-eabi --with-system-zlib --with-gnu-as --with-gnu-ld --with-pkgversion=15:10.3-2021.07-4 --without-included-gettext --prefix=/usr/lib --infodir=/usr/share/doc/gcc-arm-none-eabi/info --htmldir=/usr/share/doc/gcc-arm-none-eabi/html --pdfdir=/usr/share/doc/gcc-arm-none-eabi/pdf --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --disable-libstdc++-v3 --host=x86_64-linux-gnu --with-headers=no --without-newlib --with-multilib-list=rmprofile,aprofile CFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-arm-none-eabi-hYfgK4/gcc-arm-none-eabi-10.3-2021.07=. -flto=auto -ffat-lto-objects -fstack-protector-strong' CPPFLAGS='-Wdate-time -D_FORTIFY_SOURCE=2' CXXFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-arm-none-eabi-hYfgK4/gcc-arm-none-eabi-10.3-2021.07=. -flto=auto -ffat-lto-objects -fstack-protector-strong' DFLAGS=-frelease FCFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-arm-none-eabi-hYfgK4/gcc-arm-none-eabi-10.3-2021.07=. -flto=auto -ffat-lto-objects -fstack-protector-strong' FFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-arm-none-eabi-hYfgK4/gcc-arm-none-eabi-10.3-2021.07=. -flto=auto -ffat-lto-objects -fstack-protector-strong' GCJFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-arm-none-eabi-hYfgK4/gcc-arm-none-eabi-10.3-2021.07=. -fstack-protector-strong' LDFLAGS='-Wl,-Bsymbolic-functions -flto=auto -Wl,-z,relro' OBJCFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-arm-none-eabi-hYfgK4/gcc-arm-none-eabi-10.3-2021.07=. -flto=auto -ffat-lto-objects -fstack-protector-strong' OBJCXXFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-arm-none-eabi-hYfgK4/gcc-arm-none-eabi-10.3-2021.07=. -flto=auto -ffat-lto-objects -fstack-protector-strong' INHIBIT_LIBC_CFLAGS=-DUSE_TM_CLONE_REGISTRY=0 AR_FOR_TARGET=arm-none-eabi-ar AS_FOR_TARGET=arm-none-eabi-as LD_FOR_TARGET=arm-none-eabi-ld NM_FOR_TARGET=arm-none-eabi-nm OBJDUMP_FOR_TARGET=arm-none-eabi-objdump RANLIB_FOR_TARGET=arm-none-eabi-ranlib READELF_FOR_TARGET=arm-none-eabi-readelf STRIP_FOR_TARGET=arm-none-eabi-strip SED=/bin/sed SHELL=/bin/sh BASH=/bin/bash CONFIG_SHELL=/bin/bash Thread model: single Supported LTO compression algorithms: zlib gcc version 10.3.1 20210621 (release) (15:10.3-2021.07-4)
09-29
<think>嗯,用户这次的问题是关于解读arm-none-eabi-gcc配置信息的含义以及解决相关问题。用户之前似乎已经在系统级别讨论了嵌入式开发工具链的配置,现在需要深入理解这些配置信息的具体含义。 从用户提问的方式看,ta应该是个嵌入式开发者,正在配置交叉编译工具链。用户可能遇到了编译问题,或者单纯想系统理解这些输出信息的含义。考虑到arm-none-eabi-gcc是专业工具,用户大概率有嵌入式开发基础。 需要重点关注三个方面:配置信息各字段的解析、常见问题场景、以及诊断方法。特别要注意那些容易导致跨平台编译失败的配置项,比如--with-cpu/--with-fpu这类硬件相关参数。 用户没有提供具体的配置信息输出样例,所以需要给出通用解读方法。应该强调几个关键点:目标处理器架构、ABI规范、浮点支持配置、线程模型异常处理机制。这些都是实际开发中最容易出问题的部分。 对于诊断建议,要给出可操作的命令模板,比如用-v查看详细编译过程,用-dumpspecs分析默认链接规则。用户可能正被隐晦的链接错误困扰,这些调试技巧会很实用。 最后的相关问题设计要有递进性:从配置解读到问题排查,再到优化调整,覆盖用户可能需要的后续知识。特别是“多核编译配置”这点,很多开发者会忽略工具链本身的并行编译能力。 </think>理解`arm-none-eabi-gcc`配置信息是嵌入式开发的关键,它能揭示工具链的目标架构、支持的指令集、默认选项等重要特性。以下是详细的解读方法常见问题解决方案: --- ### **一、配置信息详解** 通过命令 `arm-none-eabi-gcc -v` 查看配置信息,典型输出如下: ```bash Thread model: single Supported LTO compression algorithms: zlib zstd gcc version 12.2.1 20221205 (ARM) [版本号] COLLECT_GCC_OPTIONS='-v' '-mcpu=cortex-m4' '-mthumb' '-mfloat-abi=hard' '-mfpu=fpv4-sp-d16' [编译选项] Configured with: /src/gcc/configure [...] \ --target=arm-none-eabi \ --with-cpu=cortex-m4 \ --with-fpu=fpv4-sp-d16 \ --with-float=hard \ --disable-nls [核心配置项] ``` #### **关键配置项解析** | **配置项** | **含义** | |----------------------|--------------------------------------------------------------------------| | `--target=arm-none-eabi` | 目标平台为ARM裸机系统,无操作系统支持 | | `--with-cpu=cortex-m4` | 默认面向Cortex-M4内核,影响指令集生成(如Thumb-2) | | `--with-fpu=fpv4-sp-d16` | 硬件浮点单元支持:单精度浮点,16个双字寄存器 | | `--with-float=hard` | 使用硬件浮点ABI(强制使用FPU指令) | | `--disable-nls` | 禁用国际化支持(节省存储空间) | | `Thread model: single` | 仅支持单线程(裸机系统常见) | > **注**:`-mcpu`, `-mthumb`等编译选项可被项目Makefile覆盖,但配置信息反映**工具链默认行为**[^1]。 --- ### **二、常见问题及解决方案** #### **1. 浮点编译错误** * **问题现象**:链接时报错 `undefined reference to powf` * **原因**:未链接数学库(`-lm`)或 FPU 配置不匹配 * **解决**: ```makefile CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 LDFLAGS += -lm -lc -lnosys # 显式链接数学库标准库 ``` #### **2. 启动文件不匹配** * **问题现象**:`Reset_Handler` 未定义或栈指针初始化失败 * **原因**:启动文件(`startup_*.s`)与目标MCU型号不符 * **解决**: ```bash arm-none-eabi-objdump -d startup_stm32f4xx.s | grep Reset_Handler # 验证启动文件 ``` #### **3. 多核支持缺失** * **问题现象**:编译 Cortex-A 应用时报 `illegal instruction` * **原因**:工具链默认配置为 Cortex-M,需添加编译参数: ```makefile CFLAGS += -mcpu=cortex-a7 -marm # 切换为ARM模式 ``` #### **4. 工具链兼容性问题** * **诊断命令**: ```bash arm-none-eabi-gcc -print-multi-lib # 查看支持的库变体 arm-none-eabi-gcc -Q --help=target # 列出所有目标相关选项 ``` --- ### **三、最佳实践建议** 1. **版本一致性**:SDK、CMSIS库、工具链需版本匹配 2. **编译选项显式化**:即使在配置中声明,项目中也应明确 `-mcpu` `-mfpu` 3. **链接脚本验证**:使用 `arm-none-eabi-ld -T script.ld --verbose` 检查内存分配 > **核心原则**:工具链配置定义**默认行为**,实际项目需通过编译参数精确控制目标特性[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值