Multiple Compilers with CMake and KDevelop

I my recent endeavor to shut down all warnings in Kig, I found myself in the need to compile the code in GCC 4.7, GCC 4.8 and Clang. It took me a couple of searches to find out exactly what I wanted and, although it is very easy to set up, it is probably not trivial to find out what to do so I'm putting up the steps here for future reference.

The whole point of this exercise is to set up the CMAKE_C_COMPILER and CMAKE_CXX_COMPILER variables for different configuration folders. You will find warnings all around CMake's documentation saying you can only set these variables before your first CMake run, so that's why you need different configuration folders. Now, assuming you already have a build folder for your default compiler (in my case, it was GCC 4.7), you can right-click on your project and go to the "Open Configuration..." menu. There, at the CMake tab, you can add a new build folder by clicking on the green plus sign next to the name of the current configuration folder.

Adding a CMake Config Directory

Adding a CMake Config Directory

A new configuration dialog will appear, where you will set up things like the prefix and the build type the same way you set those up for the default build folder. The main difference is that you will add CMake arguments as follows (this is an example to configure GCC 4.8): -DCMAKE_C_COMPILER:STRING=/usr/bin/gcc-4.8-pre9999 -DCMAKE_CXX_COMPILER:STRING=/usr/bin/g++-4.8-pre9999

New CMake Build Directory Configuration

Configuration for a new build directory for CMake

After clicking Ok you will see the window in the brackground configuring the new build folder which will use your new compiler. You can switch back to the previous compiler by opening the configuration window and switching to the old folder.