19

I'm surprised I couldn't find this googling. I only found this, but I want to known how to find what compiler is cmake using in general? I also found this but I assume there is some variable in cmake that just holds the compiler name, right?

Hakaishin
  • 2,550
  • 3
  • 27
  • 45
  • 1
    `CMAKE_C_COMPILER` holds the name of the compiler. In general cmake uses [this script](https://github.com/Kitware/CMake/blob/master/Modules/CMakeDetermineCCompiler.cmake) to find the compiler. – KamilCuk Jul 03 '18 at 12:14
  • It depends of what do you want to do with the information (just print it, set compiler flags, etc). Can you please give an example of your CMake code in question (what have so far) and a concrete problem you want to solve? – Florian Jul 03 '18 at 12:28
  • 1
    I'm filling a bug report and it asks be for the compiler used to compile the code. So just printing the name is enough. This worked, if you could post it as an answer I could accept it. – Hakaishin Jul 03 '18 at 12:32

3 Answers3

22

You can see what variables are available in your CMake's binary output directory CMakeFiles/[your CMake's version]/CMakeCXXCompiler.cmake.

If you just want to print it, your are looking for CMAKE_CXX_COMPILER_ID and CMAKE_CXX_COMPILER_VERSION. Those are available cross-platform.

Here are two examples of what CMake detects and generates from my projects:

set(CMAKE_CXX_COMPILER_ID "GNU")
set(CMAKE_CXX_COMPILER_VERSION "4.6.3")

Or

set(CMAKE_CXX_COMPILER_ID "MSVC")
set(CMAKE_CXX_COMPILER_VERSION "19.0.24215.1")

Other kind of variables are there to check for platforms/toolchains like CMAKE_COMPILER_IS_GNUCXX.

Florian
  • 39,996
  • 9
  • 133
  • 149
  • I am using Android Studio. How do I find the CMakeFiles directory you mention? – ScottK Jul 29 '20 at 18:01
  • 4
    Print Compiler Version: `message("Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")` – Ito Oct 27 '20 at 20:10
  • @Ito You can find the directories and files under the .cxx generated folder – Joseph Oct 19 '21 at 09:27
  • I just discovered that `CMAKE_CXX_COMPILER_VERSION` contains the compiler version in use when I did the first CMake call in the build folder. Since then system updates upgraded the compiler but `CMAKE_CXX_COMPILER_VERSION` still **contains the old version while in fact using the new version**! IMHO this makes the variable useless. – bjhend Mar 24 '22 at 11:39
5

I am not quite sure if I understood your question precisely, but if you just want to know which compiler is being used, enable a verbose build with the CMAKE_VERBOSE_MAKEFILE option:

cmake .. -DCMAKE_VERBOSE_MAKEFILE=ON

and then run make as usual. This will show which commands are used for building your code.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Sergio Losilla
  • 730
  • 1
  • 5
  • 14
0

You can check which compiler is being used by CMake by looking at the

CMakeCache.txt

CMake generates a cache file (usually named CMakeCache.txt) in the build directory. This file stores various configuration settings, including the compiler being used. To check the compiler you can search for the term CMAKE_C_COMPILER or CMAKE_CXX_COMPILER it includes the path to the current compiler, you are configuring your project with.