11

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.

phoenix
  • 7,988
  • 6
  • 39
  • 45
jkj yuio
  • 2,543
  • 5
  • 32
  • 49

2 Answers2

13

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.

Community
  • 1
  • 1
marco.m
  • 4,573
  • 2
  • 26
  • 41
  • There may be issues getting this to work in the latest version of CLion: https://youtrack.jetbrains.com/issue/CPP-7800 – phoenix Nov 18 '16 at 14:02
  • 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. – rubenvb Nov 28 '16 at 16:44
  • Instead of writing a path for me it worked if I just wrote "clang". (writing the path did not work for me). – Mark Apr 11 '17 at 18:56
0

In fact the latest version of Clions 2018.2 running on windows 10 environment work with LLVM clang version 6 /6.0.1 or even 7.0 along with GCC mingw x64 win32 specific variant.

linker default is set to GCC not visual studio

I guess it should work on cygwin too with the same setup as the following also tested to work on a number of popular c++ IDE also.

x64 or 32 specific GCC mingw version tested to work on Clions 2018.2

\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0 or mingw-w64\i686-8.1.0-win32-dwarf-rt_v6-rev0

CMake build setup as below

cmake_minimum_required(VERSION 3.10)
project(project_name )

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_C_COMPILER "c:/llvm/bin/clang.exe")
set(CMAKE_CXX_COMPILER "c:/llvm/bin/clang++.exe")
// target i686-pc-windows-gnu for 32bit 
set(CL_COVERAGE_COMPILE_FLAGS "-v -target x86_64-pc-windows-gnu -Wall -Wextra -std=gnu++17")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${CL_COVERAGE_COMPILE_FLAGS}" )
add_executable(project_name   yourcpp.cpp)