6

I'm trying to get a simple 'Hello World' program to build on Windows 10, preferably using CMake and clang. I can successfully compile, link and run the same project if I use the g++ compiler from MinGW, but have problems when I try using clang++.

I have CMake, MinGW and LLVM already installed and accessible in my path:

clang++
clang++: error: no input files
cmake --version
cmake version 3.16.0-rc1

I have set up environment variables for CMake to use clang:

echo %CC%
C:\Program Files\LLVM\bin\clang.exe
echo %CXX%
C:\Program Files\LLVM\bin\clang++.exe

Now when I run cmake with my simple "Hello World" C++ project, cmake complains about not being able to use clang:

cmake -G "MinGW Makefiles" ..
-- The CXX compiler identification is Clang 9.0.0 with GNU-like command-line
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang++.exe
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang++.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.16/Modules/CMakeTestCXXCompiler.cmake:53 (message):
  The C++ compiler

    "C:/Program Files/LLVM/bin/clang++.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/pball/git/bchest/build/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/mingw32-make.exe cmTC_838da/fast && C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/mingw32-make.exe -f CMakeFiles\cmTC_838da.dir\build.make CMakeFiles/cmTC_838da.dir/build
    mingw32-make.exe[1]: Entering directory 'C:/Users/pball/git/bchest/build/CMakeFiles/CMakeTmp'
    Building CXX object CMakeFiles/cmTC_838da.dir/testCXXCompiler.cxx.obj
    C:\PROGRA~1\LLVM\bin\CLANG_~1.EXE    -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd   -o CMakeFiles\cmTC_838da.dir\testCXXCompiler.cxx.obj -c C:\Users\pball\git\bchest\build\CMakeFiles\CMakeTmp\testCXXCompiler.cxx
    Linking CXX executable cmTC_838da.exe
    "C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_838da.dir\link.txt --verbose=1
    C:\PROGRA~1\LLVM\bin\CLANG_~1.EXE -fuse-ld=lld-link -nostartfiles -nostdlib   -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd    @CMakeFiles\cmTC_838da.dir\objects1.rsp  -o cmTC_838da.exe -Xlinker /implib:cmTC_838da.lib -Xlinker /pdb:C:\Users\pball\git\bchest\build\CMakeFiles\CMakeTmp\cmTC_838da.pdb -Xlinker /version:0.0  @CMakeFiles\cmTC_838da.dir\linklibs.rsp
    lld-link: error: could not open 'kernel32.lib': no such file or directory
    lld-link: error: could not open 'user32.lib': no such file or directory
    lld-link: error: could not open 'gdi32.lib': no such file or directory
    lld-link: error: could not open 'winspool.lib': no such file or directory
    lld-link: error: could not open 'shell32.lib': no such file or directory
    lld-link: error: could not open 'ole32.lib': no such file or directory
    lld-link: error: could not open 'oleaut32.lib': no such file or directory
    lld-link: error: could not open 'uuid.lib': no such file or directory
    lld-link: error: could not open 'comdlg32.lib': no such file or directory
    lld-link: error: could not open 'advapi32.lib': no such file or directory
    lld-link: error: could not open 'oldnames.lib': no such file or directory
    lld-link: error: could not open 'msvcrtd.lib': no such file or directory
    CLANG_~1: error: linker command failed with exit code 1 (use -v to see invocation)
    mingw32-make.exe[1]: *** [CMakeFiles\cmTC_838da.dir\build.make:88: cmTC_838da.exe] Error 1
    mingw32-make.exe[1]: Leaving directory 'C:/Users/pball/git/bchest/build/CMakeFiles/CMakeTmp'
    mingw32-make.exe: *** [Makefile:120: cmTC_838da/fast] Error 2

This is a freshly installed Windows 10 PC. It has no Visual Studio nor any Microsoft development tool installed on it. If possible I would prefer not having to install the Visual Studio for example to get the msvcrtd.lib. I am using VS Code at the moment, but this should be independent of the IDE being used.

My question is, what exactly do I have to install apart from LLVM, CMake and MinGW to make my first simple C++ project to build?

Peter
  • 63
  • 1
  • 1
  • 5

4 Answers4

5

You are missing the libraries in the linker flag. These libraries may be found in the following location:

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17134.0\um\x86

The exact path on your system may vary depending on the OS version etc., but you get the idea i believe. After finding the location you can add the path to the compiler flag in the CMakeLists.txt file e.g.,

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xlinker /libpath:path_to_library")

See related answers:

https://software.intel.com/en-us/forums/intel-fortran-compiler/topic/784047

https://stackoverflow.com/a/48576249/811335

How /libpath flag is used:

https://learn.microsoft.com/en-us/cpp/build/reference/libpath-additional-libpath?view=vs-2019

A. K.
  • 34,395
  • 15
  • 52
  • 89
  • I tried this approach, but it seems CMake is not using the `CMAKE_CXX_FLAGS` flags when it runs the compiler test. The compiler test is run before any kind of actual build steps, even before the makefiles are generated by cmake. – Peter Nov 24 '19 at 15:51
  • 1
    Cmake does the test in the project command. Also, it initializes the CMAKE_CXX_FLAGS from CXX_FLAGS env var. Try to set it that way. – Fabian Keßler Dec 02 '21 at 22:12
  • In my case, not trying to get rid of MSVC libraries as I am already using those as well. I just want to be able to build with `clang-cl` too. I invoke cmake as `cmake.exe --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:/dev/LLVM/bin/clang-cl.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:/dev/LLVM/bin/clang-cl.exe "-DCMAKE_CXX_FLAGS:STRING=-Xlinker /libpath:C:/Program Files/Microsoft Visual Studio/2022/Community/SDK/ScopeCppSDK/vc15/SDK/lib" -Sc:/dev/work/cpp-playground -Bc:/dev/work/cpp-playground/build -G Ninja` but no luck. – meguli Sep 05 '22 at 18:25
2

To force Clang to use its own libraries instead of MSVC's, add "-target x86_64-w64-mingw32" to CMAKE_C(XX)_FLAGS.

Beware: you have to do this before CMake identifies the compiler, e.g. either before the first C or C++ project definition in CMake (e.g. before the project() call):

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target x86_64-w64-mingw32")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -target x86_64-w64-mingw32")

project(MyProject ...)

Alternatively, you can pass it with "-D" to CMake on the command line.

Tested with clang 10.0

ocroquette
  • 3,049
  • 1
  • 23
  • 26
  • This solution spits out warnings for me like: `clang-cl: warning: unknown argument ignored in clang-cl: '-lkernel32'`, the test project is not compiled either – Alexey S. Larionov May 23 '23 at 11:03
1

If I remember correctly, Clang attempts to use MSVC's standard library on Windows by default, since Clang's own standard library doesn't work on Windows yet.

If you don't have MSVC installed, this causes problems.

The easiest solution is to install MSYS2 and use MSYS2's patched Clang, which uses GCC's libraries by default. As a nice bonus, MSYS2 also comes with an up-to-date GCC version.


Alternatively, you can use -target flag to tell Clang to use GCC's libraries. If I remember correctly, this is done by adding -target x86_64-w64-mingw32 to both compiler and linker flags.

(If it doesn't work, try -target x86_64-w64-windows-gnu, I can't remember which one it is. Replace x86_64 with i686 if you're using a 32-bit compiler.)

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • The question is specific to using CMake with clang. Your answer has nothing to do with cmake. – Former contributor Nov 17 '19 at 21:39
  • 3
    @Pedro That's because OP's problem has nothing to do with CMake, if I'm correct. – HolyBlackCat Nov 17 '19 at 21:40
  • I think you are not – Former contributor Nov 17 '19 at 22:07
  • 1
    @Pedro I'm not 100% sure about it. Let's see if it works for OP. – HolyBlackCat Nov 17 '19 at 22:20
  • @HolyBlackCat I tried adding the -target option as you suggested to the CMAKE_CXX_FLAGS variable, but CMake is not using those flags when it is testing the compiler whether it works or not. The compiler test still fails with the same error as before. However, when I run clang++ ---version, the target indeed seems to be incorrect, this is among the output: `Target: x86_64-pc-windows-msvc` – Peter Nov 19 '19 at 02:13
  • @Peter Try adding it to both `CMAKE_CXX_FLAGS` and `CMAKE_EXE_LINKER_FLAGS` and `CMAKE_SHARED_LINKER_FLAGS` (alternatively, try setting `CMAKE_CXX_COMPILER` to `clang++ -target blah`). If it doesn't work, I suggest installing MSYS2. It's much easier that configuring the official Clang, *and* it also gives you a linux-like build environment (so you can build autotools-based projects too). I'm successfully using it right now, and Clang works with CMake out of the box. – HolyBlackCat Nov 19 '19 at 10:47
  • @HolyBlackCat I ended up installing MSYS2 and it seems to be working now. I still had to specify the clang++ compiler in a `CXX` environment variable, and I still had to have mingw installed as well as msys2, but now I can compile and link a c++ program. Thanks! – Peter Nov 24 '19 at 15:53
  • 2
    @Peter Glad it worked. But a separate MinGW installation shouldn't be necessary, as MSYS2 installs a it's own MinGW GCC as a dependency for Clang. – HolyBlackCat Nov 24 '19 at 16:36
-1

Your Clang compiler is probably built to target the MSVC ABI. If I try your scenario, this is my error message:

-- The CXX compiler identification is Clang 9.0.0 
CMake Error at C:/Program Files/CMake/share/cmake-3.13/Modules/CMakeDetermineCompilerId.cmake:802 (message):   The Clang compiler tool

    "C:/Program Files/LLVM/bin/clang++"

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

This was with CMake 3.13 and LLVM 9.0, and trying to use -G "MinGW Makefiles". It works using this:

SET CXX="C:/Program Files/LLVM/bin/clang-cl.exe"
cmake -G "NMake Makefiles" ..

You probably don't need to install Visual Studio, but if you want to use nmake and the MSVC libraries, you definitely need the Windows 10 SDK which is a big download, and it will be installed as well if you decide to install Visual Studio.

On the other hand, The problem seems to be related to the CMake 3.16 version. The above unsucessful tests were made with cmake 3.13, but after that I've upgraded to CMake 3.15.5 and it works perfectly. Here are the exact versions:

> cmake -version
cmake version 3.15.5
> clang++  --version
clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

My build.cmd script:

SET CXX="C:/Program Files/LLVM/bin/clang++.exe"
cmake -G "MinGW Makefiles" ..

So the problem may be with your CMake version, or the MinGW libraries, as your error message is suggesting.

Former contributor
  • 2,466
  • 2
  • 10
  • 15
  • 1
    On Windows, Clang can switch between using MSVC's and MinGW's ABIs and standard libraries, with a single flag (`-target x86_64-w64-mingw32` or `-target x86_64-w64-windows-gnu`, not sure which one). With that, Windows SDK is unnecessary (GCC's libraries & headers will be used instead). – HolyBlackCat Nov 17 '19 at 21:01