Search code examples
c++cmakeclangclionmingw-w64

clang++ in CLion Windows, how to run with MinGW--w64 instead of Windows VC?


What I have

  • Windows 10 x64
  • MinGW-w64 toolchain 7.3.0, 32+64, 64bit version set in PATH
  • CLang Windows distribution 6.0.0, 32+64, 64bit version set in PATH
  • CLion 2018.1 & built-in cmake
  • Set CMake preference inside CLion following this post
  • Will not installed VS or VS build tools.
  • Can compile cpp files from command line with both g++ and clang++ without error. For clang++ I have to set flag -target x86_64-pc-mingw64 due to lack of VC toolchain and headers.

What I want

Set a functional CMake profile using clang++ compiler.

I have set a CMake profile according to the link above. However, clion give error

The Clang compiler tool
"C:/Program Files/LLVM/bin/clang.exe"

targets the MSVC ABI but has a GNU-like command-line interface.  This is
not supported.  Use 'clang-cl' instead, e.g.  by setting 'CC=clang-cl' in
the environment.  Furthermore, use the MSVC command-line environment.

If I set C and CXX COMPILERs to clang-cl instead of setting TOOLCHAIN PREFIX to LLVM, there will be an error.

CMake Error at C:/Program Files/JetBrains/CLion 
2018.1/bin/cmake/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 
(message):
The C compiler

"C:/Program Files/LLVM/bin/clang-cl.exe"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: C:/Users/huang/Documents/Programs/grade/cmake-build-release-
clang-1/CMakeFiles/CMakeTmp

Run Build Command:"C:/mingw64/bin/mingw32-make.exe" "cmTC_181bf/fast"
C:/mingw64/bin/mingw32-make.exe -f CMakeFiles\cmTC_181bf.dir\build.make 
CMakeFiles/cmTC_181bf.dir/build
mingw32-make.exe[1]: Entering directory 
'C:/Users/huang/Documents/Programs/grade/cmake-build-release-clang-
1/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_181bf.dir/testCCompiler.c.obj
C:\PROGRA~1\LLVM\bin\clang-cl.exe  /nologo   /DWIN32 /D_WINDOWS /W3  /MDd 
/Zi /Ob0 /Od /RTC1   /FoCMakeFiles\cmTC_181bf.dir\testCCompiler.c.obj 
/FdCMakeFiles\cmTC_181bf.dir/ -c 
C:\Users\huang\Documents\Programs\grade\cmake-build-release-clang-
1\CMakeFiles\CMakeTmp\testCCompiler.c
Linking C executable cmTC_181bf.exe
"C:\Program Files\JetBrains\CLion 2018.1\bin\cmake\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_181bf.dir\link.txt --verbose=1
"C:\Program Files\JetBrains\CLion 2018.1\bin\cmake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_181bf.dir --manifests  -- CMAKE_LINKER-NOTFOUND /nologo @CMakeFiles\cmTC_181bf.dir\objects1.rsp  /out:cmTC_181bf.exe /implib:cmTC_181bf.lib /pdb:C:\Users\huang\Documents\Programs\grade\cmake-build-release-clang-1\CMakeFiles\CMakeTmp\cmTC_181bf.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib 
RC Pass 1: command "rc /foCMakeFiles\cmTC_181bf.dir/manifest.res CMakeFiles\cmTC_181bf.dir/manifest.rc" failed (exit code 0) with the following output:
缁崵绮洪幍鍙ョ瑝閸掔増瀵氱�规氨娈戦弬鍥︽閵嗕慷ingw32-make.exe[1]: *** [CMakeFiles\cmTC_181bf.dir\build.make:98: cmTC_181bf.exe] Error -1
mingw32-make.exe[1]: Leaving directory 'C:/Users/huang/Documents/Programs/grade/cmake-build-release-clang-1/CMakeFiles/CMakeTmp'
mingw32-make.exe: *** [Makefile:125: cmTC_181bf/fast] Error 2

CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)

I guess it may be because I haven't set the target as I will do in the command line, and the default target is VC. In CLion interface, how should I do it?


Solution

  • There is a combination of 2 problems:

    1. CMake doesn't support the native clang++.exe driver (see the related ticket).
    2. Even if it would, I don't think MinGW could work with the installed clang distribution. You'll have to install clang as a MinGW package (see e.g. this readme, here is the specific package).

    For now there is no specific support for this use case in CLion itself (so it should work when everything is set up properly, but it doesn't offer any help to do it).