Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this #ifdef __GNUC__ about?

Tags:

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?

like image 625
alvas Avatar asked Nov 11 '13 14:11

alvas


People also ask

Can Google identify things?

You can use Google Lens to identify real-world objects with your camera and find information about plants, animals, landmarks, restaurants, products, and more.

Can I take a picture of something and you can tell me what it is?

Google Goggles is now Google Lens In August 2018, Google Goggles was retired and replaced by Google Lens. The idea is the same: Someone can view or take a picture of an item and Google will tell you what it is.


2 Answers

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 
like image 121
masoud Avatar answered Sep 16 '22 14:09

masoud


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.

like image 45
mah Avatar answered Sep 19 '22 14:09

mah