Determining the C Standard Implemented by a Compiler
Detecting the version of the C standard used by a compiler helps ensure compatibility and efficient code development. Here's how to accomplish it:
Using the __cplusplus Macro
C 11 introduced the __cplusplus macro, which reflects the current C standard. For example, in C 11, __cplusplus will be 201103L. However, some compilers like gcc and MSVC may not always set this macro correctly.
Compiler-Specific Checks
If __cplusplus isn't sufficient, resort to compiler-specific checks. For example, clang defines __clang_major__, __clang_minor__, and __clang_patchlevel__. Similarly, MSVC defines _MSC_VER.
Feature Comparison
Another approach is to compare implemented features against a known C standard. Tools like Cppcheck or Static ReSharper can analyze code and report supported features, enabling identification of the implemented standard.
Boost.Config
Boost.Config provides specific macros that indicate the C standard version, such as BOOST_CXX_STD_11, BOOST_CXX_STD_14, and so on.
Older Compilers
For older compilers that may not support the aforementioned methods, consider using compiler version-specific hacks or Boost.Config to detect supported features and work around missing ones.
Feature-Specific Checking
For precise feature checks, consider using the compiler's build system. Run code that uses specific features and examine compilation results. Autoconf is a popular tool for this approach.
Compiler Support Matrix
The Stdcxx Wiki provides a comprehensive matrix of compiler support for C 0x features. However, manual feature checking is recommended for finer-grained analysis.
The above is the detailed content of How Can I Determine the C Standard My Compiler Is Using?. For more information, please follow other related articles on the PHP Chinese website!