Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a simple C++ project on Windows, using CMake and clang

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?

like image 307
Peter Avatar asked Nov 17 '19 12:11

Peter


3 Answers

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://docs.microsoft.com/en-us/cpp/build/reference/libpath-additional-libpath?view=vs-2019

like image 174
A. K. Avatar answered Sep 26 '22 02:09

A. K.


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

like image 37
ocroquette Avatar answered Sep 23 '22 02:09

ocroquette


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.)

like image 38
HolyBlackCat Avatar answered Sep 26 '22 02:09

HolyBlackCat