在我的c/c++中如何区分代码,在GCC和Clang之间作为编译器?

发布于 2025-02-05 03:44:46 字数 126 浏览 2 评论 0原文

我通常通过检查__ GNUC __来编写涉及GCC的代码。现在,我想编写一些对GCC和Clang行为不同的代码 - 但似乎Clang也定义了__ GNUC __。我如何区分它们?

I usually write GCC-targeted code by checking __GNUC__. Now I want to write some code which behaves differently for GCC and for clang - but it seems clang defines __GNUC__ as well. How do I differentiate between them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亣腦蒛氧 2025-02-12 03:44:47

两者都定义__ gnuc __,但只有clang __ clang __

因此,对于仅GCC而言,您会写:

#if defined(__GNUC__) && !defined(__clang__)
// do GCC-only stuff
#endif

当然,您可以通过分开这两个条件来适应仅GCC,仅叮当响。

注意:对于C和C ++,这是相同的。

While both define __GNUC__, only clang defines __clang__.

So, for GCC-only, you would write:

#if defined(__GNUC__) && !defined(__clang__)
// do GCC-only stuff
#endif

and of course you could adapt this to GCC-only, clang-only and other, by separating the two conditions.

Note: This is the same for C and for C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文