1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C++コンパイラがgccかclangかそれ以外かをコード側から判別して条件分岐

Posted at

共通ライブラリを提供したいのに、GCCコンパイラだけの仕様とか、Clangコンパイラにバグがあるとか、色々な理由でコンパイラごとにコードを分けたい場合がありました。その場合の対策です。

#ifdef __clang__
  // Clang compiled code
#elif defined __GNUC__
  // GCC compiled code
#elif defined _MSC_VER
  // Microsoft Visual C++ compiled code
#else
  // Others
#endif

ところで、GCCとそれ以外でコードを分けたい場合は、__GNUC__の判別だけではだめなので(Clangでも__GNUC__は定義されている)、次のようにすると出来ます。

#if !defined(__clang__) && defined(__GNUC__)
// GCC compiled code
#else
// Others
#endif

参考記事:
https://gist.github.com/treastrain/483a2d233560dc99fda8e59b6fd03361
https://qiita.com/D-3/items/b98b63c2629496856654
https://secret-garden.hatenablog.com/entry/2015/07/17/000000
https://qiita.com/naohikowatanabe/items/a4e1a3f8080210257a42

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?