C语言 如何通过 CompilerType #ifdef ?GCC 或 VC++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15127522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to #ifdef by CompilerType ? GCC or VC++
提问by cnd
I used #ifdef Win32for safe calls alike sprintf_sbut now I want to build project with MinGW and it's just wrong now. I need to use #ifdef VC++or somehow like that. Is it possible?
我曾经#ifdef Win32用于类似的安全调用,sprintf_s但现在我想用 MinGW 构建项目,但现在是错误的。我需要使用#ifdef VC++或以某种方式那样。是否可以?
采纳答案by Kevin Richardson
See the "Microsoft-Specific Predefined Macros" table of Visual C predefined macros
请参阅Visual C 预定义宏的“Microsoft 特定预定义宏”表
You could check for _MSC_VER.
你可以检查一下_MSC_VER。
回答by Aniket Inge
#ifdef __clang__
/*code specific to clang compiler*/
#elif __GNUC__
/*code for GNU C compiler */
#elif _MSC_VER
/*usually has the version number in _MSC_VER*/
/*code specific to MSVC compiler*/
#elif __BORLANDC__
/*code specific to borland compilers*/
#elif __MINGW32__
/*code specific to mingw compilers*/
#endif
回答by autistic
Preferably, you should resort to using portable symbols. I understand sometimes those symbols may not be defined, so you can see the Predef projectfor an extensive list of preprocessor macros regarding standards, compilers, libraries, operating systems and architectures that aren'tportable.
最好,您应该求助于使用便携式符号。我知道有时可能未定义这些符号,因此您可以查看Predef 项目以获取有关标准、编译器、库、操作系统和不可移植体系结构的大量预处理器宏列表。
However, the function you specifically mention in this question has been included within the C11 standard as a part of Annex K.3, the bounds-checking interfaces (library).
但是,您在此问题中特别提到的功能已作为附件 K.3 边界检查接口(库)的一部分包含在 C11 标准中。
K.3.1.1p2states:
K.3.1.1p2指出:
The functions, macros, and types declared or defined in K.3 and its subclauses are declared and defined by their respective headers if
__STDC_WANT_LIB_EXT1__is defined as a macro which expands to the integer constant 1 at the point in the source file where the appropriate header is first included
在 K.3 及其子条款中声明或定义的函数、宏和类型由它们各自的头文件声明和定义,如果
__STDC_WANT_LIB_EXT1__被定义为一个宏,该宏在源文件中适当头文件所在的点处扩展为整数常量 1首先包括
Thus, you should place preference upon checking __STDC_WANT_LIB_EXT1__, and only use compiler-specific symbols when that doesn't exist.
因此,您应该优先检查__STDC_WANT_LIB_EXT1__,并且仅在不存在时才使用特定于编译器的符号。

