Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test the current version of GCC at compile time?

Tags:

c++

c

gcc

versions

I would like to include a different file depending on the version of GCC. More precisely I want to write:

#if GCC_VERSION >= 4.2 #  include <unordered_map> #  define EXT std #elif GCC_VERSION >= 4 #  include <tr1/unordered_map> #  define EXT std #else #  include <ext/hash_map> #  define unordered_map __gnu_cxx::hash_map #  define EXT __gnu_cxx #endif 

I don't care about gcc before 3.2.

I am pretty sure there is a variable defined at preprocessing time for that, I just can't find it again.

like image 784
PierreBdR Avatar asked Nov 03 '08 16:11

PierreBdR


People also ask

How do I check my gcc version?

So if you ever need to check the version of the GCC C++ compiler that you have installed on your PC, you can do it through the command prompt by typing in the single line, g++ --version, and this will return the result.

What is the current gcc version?

The current version is 11.

How do I know what version of G ++ compiler I have?

Just use g++ -v or gcc -v which will give you your compiler version. You can also go to your windows settings, click on "Apps" go to the search bar and search up c++ scroll down to the last item, and click on it. The version should be displayed fairly obviously for you.

How do I know what version of c compiler I have?

Type “gcc –version” in command prompt to check whether C compiler is installed in your machine.


2 Answers

There are a number of macros that should be defined for your needs:

__GNUC__              // major __GNUC_MINOR__        // minor __GNUC_PATCHLEVEL__   // patch 

The version format is major.minor.patch, e.g. 4.0.2

The documentation for these can be found here.

like image 62
luke Avatar answered Oct 04 '22 19:10

luke


Ok, after more searches, it one possible way of doing it is using __GNUC_PREREQ defined in features.h.

#ifdef __GNUC__ #  include <features.h> #  if __GNUC_PREREQ(4,0) //      If  gcc_version >= 4.0 #  elif __GNUC_PREREQ(3,2) //       If gcc_version >= 3.2 #  else //       Else #  endif #else //    If not gcc #endif 
like image 29
PierreBdR Avatar answered Oct 04 '22 19:10

PierreBdR