You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCC编译报错时如何抑制宏展开回溯信息?

如何在GCC 8.2.0(MinGW.org)中抑制递归宏编译错误的重复"in expansion of"注释?

我在代码中偶尔会使用递归宏生成样板代码,当宏生成的代码出现编译错误时,编译器会输出大量重复的“in expansion of”注释信息(示例如下),这些信息对定位问题毫无帮助,仅需前几行即可。想了解在GCC 8.2.0(MinGW.org GCC-8.2.0-3)中是否可以抑制这类注释?

In file included from ../../include/pw/hsm/macros.hpp:8,
                 from ../../include/pw/hsm/includes.hpp:7,
                 from ../common.hpp:6,
                 from common.hpp:9,
                 from StateMachine.hpp:4,
                 from main.cpp:1:
../../include/pw/hsm/macros.hpp:74:33: note: candidate: 'virtual pw::hsm::return_type lamp::_StateMachine_VISITOR::visit(const lamp::EPowerButtonPressed&)'
  virtual ::pw::hsm::return_type visit(const name_& e) { return ::pw::hsm::PASS; }
                                 ^~~~~
../../lib/map-macro/map.h:16:20: note: in definition of macro 'EVAL0'
 #define EVAL0(...) __VA_ARGS__
                    ^~~~~~~~~~~
../../lib/map-macro/map.h:17:26: note: in expansion of macro 'EVAL0'
 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
                          ^~~~~
../../lib/map-macro/map.h:17:32: note: in expansion of macro 'EVAL0'
 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
                                ^~~~~
../../lib/map-macro/map.h:18:20: note: in expansion of macro 'EVAL1'
 #define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
                    ^~~~~
../../lib/map-macro/map.h:17:20: note: in expansion of macro 'EVAL0'
 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
                    ^~~~~
../../lib/map-macro/map.h:17:26: note: in expansion of macro 'EVAL0'
 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
                          ^~~~~
../../lib/map-macro/map.h:17:32: note: in expansion of macro 'EVAL0'
 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
                                ^~~~~
../../lib/map-macro/map.h:18:26: note: in expansion of macro 'EVAL1'
 #define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))

...

可以通过以下编译选项解决这个问题:

  • -fmacro-backtrace-limit=N:这个选项能限制宏展开回溯信息的输出行数,完全适配GCC 8.2.0版本:
    • 设为-fmacro-backtrace-limit=0:直接禁止所有宏展开的回溯注释,彻底消除这类冗余信息;
    • 设为-fmacro-backtrace-limit=1:仅保留最顶层的宏展开关联信息,不会递归输出深层的宏调用链,既保留必要定位线索,又避免重复内容。

把这个参数添加到你的编译命令、Makefile的CFLAGS/CXXFLAGS中即可生效。

注意:-Wno-macro-redefined这类选项是用于抑制宏重定义警告的,和宏展开回溯信息无关,不要混淆使用。


内容的提问来源于stack exchange,提问作者Patrick Wright

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 13:55:17