如何暂时禁用 C/C++ 中的宏扩展?

2021-12-22 00:00:00 gcc visual-c++ c++ c-preprocessor

出于某种原因,我需要暂时禁用头文件中的一些宏,#undef MACRONAME 将使代码编译,但它会取消现有宏的定义.

For some reason I need to temporarily disable some macros in a header file and the #undef MACRONAME will make the code compile but it will undef the existing macro.

有没有办法禁用它?

我应该提到您并不真正了解宏的值,而且我正在寻找交叉编译器解决方案(至少应该在 GCC 和 MSVC 中工作).

I should mention that you do not really know the values of the macros and that I'm looking for a cross compiler solution (should work at least in GCC and MSVC).

推荐答案

在 MSVC 中,您可以使用 push_macro pragma, GCC 支持它与 Microsoft Windows 编译器的兼容性.

In MSVC you could use push_macro pragma, GCC supports it for compatibility with Microsoft Windows compilers.

#pragma push_macro("MACRONAME")
#undef MACRONAME

// some actions

#pragma pop_macro("MACRONAME")

相关文章