How to switch between GCC and Clang in Clion from within CMakeLists.txt using windows/cygwin

19,762

Specifying a compiler with CMake is a bit delicate. Although the method you are using, setting CMAKE_CXX_COMPILER in CMakeLists.txt works, it is the least-recommended way in the CMake FAQ.

CLion supports method 2 of the CMake FAQ: using -D within the cmake invocation. Setting the variables in CMakeLists.txt has no effect.

On Mac, go to Preferences

On Linux/Windows, go to File | Settings

then Build, Execution, Deployment | CMake | CMake options and enter the text:

-D CMAKE_C_COMPILER=/path/to/c_compiler
-D CMAKE_CXX_COMPILER=/path/to/c++_compiler

See the CLion FAQ for details.

Note also that when you change compilers, you will have to invalidate the CLion cmake cache and restart, see my answer on How to clear CMake cache in Clion?.

EDIT

After I wrote this answer, CLion added support for multiple build directories, as pointed out by @rubenvb in the comments. This is another path to investigate.

Share:
19,762

Related videos on Youtube

jkj yuio
Author by

jkj yuio

Android developer

Updated on September 14, 2022

Comments

  • jkj yuio
    jkj yuio almost 2 years

    I put

     set(CMAKE_CXX_COMPILER           "/usr/bin/clang.exe")
    

    Run/Clean, Run/Build.

    I get link errors like:

    undefined reference to `std::ios_base::Init::~Init()'
    : undefined reference to `__gxx_personality_v0'
    

    Presumably there are other variables to change. Tried adding -lstdc++ to CMAKE_CXX_FLAGS, but no different.

    Is there a CLion way as opposed to a CMake way, for example?

    Thanks.

    • Anya Shenanigans
      Anya Shenanigans about 9 years
      the C++ compiler is called clang++, you're using the C compiler
  • phoenix
    phoenix over 7 years
    There may be issues getting this to work in the latest version of CLion: youtrack.jetbrains.com/issue/CPP-7800
  • rubenvb
    rubenvb over 7 years
    Note that you can have two separate build directories (one per compiler) so the switching becomes a lot less painful and a full rebuild isn't needed each time you switch.
  • Mark
    Mark about 7 years
    Instead of writing a path for me it worked if I just wrote "clang". (writing the path did not work for me).