Recognizing clang, gcc, and tcc by implementation-defined macros
Asked Answered
G

1

8

I use clang, gcc, and tcc, and I'd like to be able to differentiate between the three in a common header.

Judging by their macro dumps, I expect that the presence of the __clang__ macro will uniquely identify clang.

I'm unable to get a macro dump with tcc ( $compiler -x c -E -dM /dev/null doesn't work in its case ).

What is the macro(s) (if any) that will uniquely identify gcc (and possibly tcc)?

Gourmandise answered 12/9, 2016 at 8:47 Comment(12)
Maybe __GNUC__?Solis
There are duplicates for gcc, you should really ask only about tcc.Asthenosphere
@KerrekSB clang defines __GNUC__ too.Gourmandise
I'm afrais clang redefine the whole set of predefined macros of gcc. So quite difficult to isolate gcc. I don't know about tcc.Spiffy
@PSkocik: Ah, no luck then. Clang isn't even licensed under a GNU license... Maybe defined(__GNU__) && !defined(__clang__)?Solis
@PSkocik if gnuc and !clangAsthenosphere
@Asthenosphere That works. I checked, and tcc doesn't define __GNUC__. Thanks.Gourmandise
Why do you want to do that? Wouldn't it be much more preferable not to rely on compiler-specific quirks and instead stick to the C++ standard? If you really have to test e.g. for the presence of a certain feature, the right question to ask would be how to do that...3d
@MartinHierholzer "If you really have to test e.g. for the presence of a certain feature, the right question to ask would be how to do that...", it would , but I'm not sure if there's a good answer to that one. I know there's autoconf, but I don't want to use anything that combines feature detection with building,& I don't want to run a script per project. Ideally, I'd want something that keeps and updates a macro-based feature list in a global header, and the features macros it would expose would be dependent on the environment (compiler, feature test macros) in which I would include it.Gourmandise
@MartinHierholzer For now, simply switching actions based on the current compiler seems easier, although it is a kind of a hack.Gourmandise
@MartinHierholzer: sticking to the C++ standard is a very bad idea for C code!Epicurean
Sorry, I misread the tag, this is about C and not C++. Still, sticking to the standard might be a good idea, if possible. It just will be a lot harder than in C++. Maybe rethink your choice of language ;-)3d
A
10

Use __TINYC__ to detect tcc1.

Detection of gcc and clang is explained in this StackOverflow question: Detect gcc as opposed to msvc / clang with macro


1 (Quoted from: http://bellard.org/tcc/tcc-doc.html#SEC9)
__TINYC__ is a predefined macro to 1 to indicate that you use TCC.

Asthenosphere answered 12/9, 2016 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.