How to set C or C++ compiler for CMake

Sometimes you might need to use a different version of a default compiler (like gcc) or a different compiler (like Intel compiler) to build your code. Specifying this is easy in CMake. Add these lines to your CMakeLists.txt:

SET(CMAKE_C_COMPILER /path/to/c/compiler)
SET(CMAKE_CXX_COMPILER /path/to/cpp/compiler)

Tried with: CMake 2.8.7 and Ubuntu 12.04 LTS


7 thoughts on “How to set C or C++ compiler for CMake”

    1. Einpoklum: Thanks for sharing that. Indeed, Method 1 and 2 listed in the FAQ are more elegant.

      However, in my case I wanted to use GCC for normal compilation and Intel compiler for my CMake project, so Method 1 was not suitable. Method 2 was not suitable cause our team was not interested in passing command line arguments to CMake.


  1. Thanks to both for the response. I have a related problem: I need to modify the compile line from ‘gcc [args]’ to ‘/usr/local/bin/fubar gcc [args]’. Where ‘fubar’ is an executable to performs instrumentation while it invokes the gcc command with [args].
    As I understand from the above replies and from the CMake FAQ, setting GCC to ‘/usr/local/bin/fubar gcc’ will NOT work since CMake treats the value of GCC as a single string that is a full path to an executable (and it checks that it exists).


  2. I want to work on project that needs make, cmake and mingw so i need to setup my environment so that cmake can access a compiler(g++ or gcc). I have install all the tools and i’m having errors tell cmake where to find the compiler either by setting the environment variable ‘CC’


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s