How to #ifdef by CompilerType ? GCC or VC++
Asked Answered
L

3

26

I used #ifdef Win32 for safe calls alike sprintf_s but 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?

Loni answered 28/2, 2013 at 4:28 Comment(2)
As an aside, you could just use snprintf() which is a standard function that does the same thing.Manlove
@Manlove Not in Visual C++, it doesn't; Alas there is only C89 support, and snprintf wasn't guaranteed to exist in C89. There's the similar _snprintf, though IIRC that wasn't renamed to snprintf because that does something subtly different.Stevens
A
12

See the "Microsoft-Specific Predefined Macros" table of Visual C predefined macros

You could check for _MSC_VER.

Adin answered 28/2, 2013 at 4:35 Comment(0)
U
59
#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
Unveiling answered 28/2, 2013 at 4:39 Comment(6)
__GNUC__ is a language dialect, not a compiler. Most compilers (Clang and ICC included) will define __GNUC__ at least sometimes.Goal
@Goal __GNUC__ is a language dialect, but the code you write is usually for the compiler and hopefully humans. However, if you meant that I have to improve this answer by changing /* code for GNU C compiler */ to /* code in GNU C Dialect */ then, you may submit a proposal for improvementUnveiling
Just pointing out that if the OP was trying to distinguish GCC from VC++ from "some other compiler also supporting GNU C" (e.g. ICC, Clang, Green Hills, ...) then checking __GNUC__ would be insufficient. In that case, this question/answer would fit their situation better.Goal
FYI on macos, clang defines the gcc flag, so you need to do some extra logic like: #if GNUC #ifndef clang #pragma GCC diagnostic warning "-Wclass-memaccess" #endif #endifStrum
Ah - I see you put clang first, thus avoiding the issue of clang enabling GNUC niceStrum
the last #else should probably have a static_assert(0,"Need compiler specific code for..."); for that day someone suddenly needs say... the intel compiler or some other compiler.Appling
A
12

See the "Microsoft-Specific Predefined Macros" table of Visual C predefined macros

You could check for _MSC_VER.

Adin answered 28/2, 2013 at 4:35 Comment(0)
S
8

Preferably, you should resort to using portable symbols. I understand sometimes those symbols may not be defined, so you can see the Predef project for an extensive list of preprocessor macros regarding standards, compilers, libraries, operating systems and architectures that aren't portable.

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.1.1p2 states:

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

Thus, you should place preference upon checking __STDC_WANT_LIB_EXT1__, and only use compiler-specific symbols when that doesn't exist.

Stevens answered 28/2, 2013 at 4:54 Comment(1)
Nice. Never heard of that link before, thanks for posting it.Condescendence

© 2022 - 2024 — McMap. All rights reserved.