26

I've found these lines in the libmagic code. What do they mean?

#ifdef __GNUC__
__attribute__((unused))
#endif 

What does __GNUC__ mean?
It seems to check whether GCC is installed.

What is __attribute__((unused))?
There's a code snippet here but no explanation: How do I best silence a warning about unused variables?

What is the difference between __GNUC__ and _MSC_VER?
There's some explanation on _MSC_VER, but what is it all about?
How to Detect if I'm Compiling Code with a particular Visual Studio version?

Finally the question:
How can I do the same #ifdef to check which compiler is compiling my code?

snowball
  • 15
  • 1
  • 5
alvas
  • 115,346
  • 109
  • 446
  • 738
  • The tag `compiler` should be applied to questions concerning the programming of compilers or for questions about the detailed inner workings of compilers. Don't use `compiler` for questions about options and settings for a particular compiler, use the name of the compiler you are interested in instead. – user207421 Nov 14 '13 at 00:35
  • This is the most bizarre question ever in the way it's formulated, but I'm coming from a past world where people used to first have a look at documentation, in a time where documentation was a thing. – Johan Boulé Aug 05 '22 at 21:06

2 Answers2

29

It's common in compilers to define macros to determine what compiler they are, what version is that, ... a portable C++ code can use them to find out that it can use a specific feature or not.

What does __GNUC__ mean?

It indicates that I'm a GNU compiler and you can use GNU extensions. [1]

 

What is __attribute__((unused))?

This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce an unused-variable-warning for this variable. [2]

 

What is the difference between __GNUC__ and _MSC_VER?

They're two unrelated macros. First one says I'm am a GNU compiler and second one says the version number of MS compilers. However, MS compilers are not suppose to support GNU extensions.

 

How can I do the same #ifdef to check whether OS is compiling my python code using GNU and MS visual studios?

#if (defined(__GNU__) && defined(_MSC_VER))
   // ...
#endif

However, there is no chance to have these conditions together!

masoud
  • 55,379
  • 16
  • 141
  • 208
  • which library does the `defined()` module come from? – alvas Nov 11 '13 at 15:25
  • 1
    `defined` is C++ keyword and it will be used in preprocessors. – masoud Nov 11 '13 at 15:30
  • 4
    I suspect that `#if (defined(__GNU__) && defined(_MSC_VER)` is pretty much equivalent to `#if 0`. – James Kanze Nov 11 '13 at 18:18
  • @JamesKanze: Do you mean no MS compiler has GNU macros? Yes, I just tied to answer the question. `_MSC_VER` and `__GNU__` together! I agree with you. – masoud Nov 11 '13 at 18:20
  • 3
    @MM. I would hope no Microsoft compiler defined `__GNU__` (and no version of g++ defines `_MSC_VER`). Such macros are normally used to test which compiler you're using, so that you can have compiler specific variants. (There are better ways of handling this, but the conditional compilation on such variables is widespread.) – James Kanze Nov 12 '13 at 09:23
  • Is there a python equivalence to do the same checks? – alvas Nov 12 '13 at 14:50
  • @alvas no, because python is defined by a reference implementation, not a standards document – Caleth Jul 08 '19 at 12:03
  • @masoud `__GNU__` is indicating the target operating system is GNU Hurd. So, what you wrote is "Are we compiling with the Microsoft Visual C++ compiler and targeting the GNU Hurd operating system ?". – Johan Boulé Aug 05 '22 at 21:15
3

Different compilers support different features, sometimes in different ways. You're finding a series of #ifdef blocks to enable support according to whatever compiler is building the code; for example the GNU compiler would automatically define __GNUC__. __CC_ARM, __ICCARM__, __GNUC__, __TASKING__ are all defined by certain compilers the project has been ported to and is interested in.

The __attribute__((unused)) entry is a GNU-specific indicator (though other compilers may support this now too) to state that the symbol it's attached to may be unused and so the compiler should warn you about that condition.

As to how to use these ifdefs to determine what compiler is building your code -- do it in the same manner that you're reading in another project for building C. These are not factors for your python code.

mah
  • 39,056
  • 9
  • 76
  • 93