用宏确定 GCC 编译器

Determine GCC Compiler with Macro

EDIT: Ha ha, search terms are a weird thing. I actually had to use the answer I got as a term for the search to finally find this question. In the spirit of Whosebug, I'll vote to close my own question as duplicate instead of deleting it, in case it'll serve as a landing point for someone else.

我正在编写几个使用 asm 的函数,但我只希望它们在使用可以使用 NASM 的编译器编译时运行。 (顺便说一下,我在使用 C++11。)

我对 asm 的整个概念有点陌生,但这就是我 认为的 我知道的:

假设以上是正确的,我可以使用什么宏来确保当且仅当我使用 GCC(或类似的)编译器或使用 NASM? 的编译器(#ELSE 将是该函数的可用虚拟版本,以确保与其他编译器的一般兼容性。)

我所知道的唯一此类宏与确定操作系统有关(例如 #IFDEF _WIN32),但在我使用 TDM-GCC 或 MinGW 进行编译的情况下,该宏将被错误地使用在 Windows.

NOTE: In case anyone wonders, the functions in question are basically "helpful, but not vital" utility functions. We don't have any plans to compile with a non-GCC compiler, but it's open source code, so we want to be considerate of others.

您可以使用 __GNUC__ 宏来识别 GCC(以及一些最兼容的编译器,如 Clang 和 Intel 的 C++ 编译器)。

MinGW(任何版本)可以检查 __MINGW32__ and/or __MINGW64__.