为啥相同的 CFLAGS 可以在 makefile 中重复两次?

Posted

技术标签:

【中文标题】为啥相同的 CFLAGS 可以在 makefile 中重复两次?【英文标题】:Why the same CFLAGS can be repeated twice in a makefile?为什么相同的 CFLAGS 可以在 makefile 中重复两次? 【发布时间】:2020-03-26 16:30:55 【问题描述】:

这是makefile的部分:

CFLAGS += -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16

CFLAGS  = -g -O2 -Wall
CFLAGS += -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16

第一行和第三行完全一样。

注释第一行后,“make”不会出错。

但是在注释第三行之后,“make”出错了。 错误:

/tmp/ccIrbEv9.s: Assembler messages:
/tmp/ccIrbEv9.s:77: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:368: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:402: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:1276: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:1305: Error: selected processor does not support `rbit r0,r3' in ARM mode
/tmp/ccIrbEv9.s:1371: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:1400: Error: selected processor does not support `rbit r0,r3' in ARM mode
/tmp/ccIrbEv9.s:1544: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:1573: Error: selected processor does not support `rbit r0,r3' in ARM mode
/tmp/ccIrbEv9.s:1643: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:1672: Error: selected processor does not support `rbit r0,r3' in ARM mode
/tmp/ccIrbEv9.s:2989: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:3021: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:3060: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:3096: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:3342: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:3371: Error: selected processor does not support `rbit r0,r3' in ARM mode
/tmp/ccIrbEv9.s:3443: Error: selected processor does not support `rbit r0,r0' in ARM mode
/tmp/ccIrbEv9.s:3472: Error: selected processor does not support `rbit r0,r3' in ARM mode
/tmp/ccIrbEv9.s:3783: Error: selected processor does not support `rbit r0,r0' in ARM mode
Makefile:27: recipe for target 'Src/stm32f7xx_hal_dma2d.o' failed 

我感到困惑。

【问题讨论】:

【参考方案1】:

CFLAGS = -g -O2 -Wall

此行覆盖第一行(注意分配),因此,如果您删除第三行,您将在没有这些选项的情况下进行编译,并应用工具链默认值。 在这种非常特殊的情况下,编译器似乎不接受默认目标的源代码。

【讨论】:

是的,rbit requires ARMv6T2 or later 与 ARM 与 Thumb2 模式无关。 ARM GCC 的默认 -march= 用于相当基线的旧 ARM ISA 版本。 (-mcpu= 设置目标拱门)【参考方案2】:

注意区别

CFLAGS += ...

和:

CFLAGS = ...

加号决定一切。它通过附加标志来修改CFLAGS 值。没有加号,值被替换,之前的值丢失。

    可以删除第一个CFLAGS += ...,因为它的值被CFLAGS = ...替换。

    CFLAGS = ... 可以删除,因为代码在没有它们的情况下也可以编译。

    不能删除最后一个 CFLAGS += ...,因为它提供了对您的代码至关重要的编译器标志。

【讨论】:

以上是关于为啥相同的 CFLAGS 可以在 makefile 中重复两次?的主要内容,如果未能解决你的问题,请参考以下文章

Makefile选项CFLAGS,LDFLAGS,LIBS

CFLAGS/CPPFLAGS/CXXFLAGS in Makefile介绍

Makefile选项CFLAGS,LDFLAGS,LIBS

由makefile 如何生成QT 的pro 文件

如何向Makefile传递参数?

Makefile选项CFLAGS,LDFLAGS,LIBS