使用 CMake 和 clang 在 Windows 上构建一个简单的 C++ 项目

Pet*_*ter 6 c++ windows cmake clang

我正在尝试在 Windows 10 上构建一个简单的“Hello World”程序,最好使用 CMake 和 clang。如果我使用 MinGW 的 g++ 编译器,我可以成功编译、链接和运行同一个项目,但是当我尝试使用 clang++ 时会出现问题。

我已经在我的路径中安装和访问了 CMake、MinGW 和 LLVM:

clang++
clang++: error: no input files
Run Code Online (Sandbox Code Playgroud)
cmake --version
cmake version 3.16.0-rc1
Run Code Online (Sandbox Code Playgroud)

我已经为 CMake 设置了环境变量以使用 clang:

echo %CC%
C:\Program Files\LLVM\bin\clang.exe
echo %CXX%
C:\Program Files\LLVM\bin\clang++.exe
Run Code Online (Sandbox Code Playgroud)

现在,当我使用简单的“Hello World”C++ 项目运行 cmake 时,cmake 抱怨无法使用 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
Run Code Online (Sandbox Code Playgroud)

这是一台全新安装的 Windows 10 PC。它没有安装 Visual Studio 或任何 Microsoft 开发工具。如果可能的话,我宁愿不必安装 Visual Studio 来获取msvcrtd.lib. 我目前正在使用 VS Code,但这应该独立于所使用的 IDE。

我的问题是,除了 LLVM、CMake 和 MinGW 之外,我还需要安装什么才能构建我的第一个简单的 C++ 项目?

A. *_* K. 5

您缺少链接器标志中的库。这些库可以在以下位置找到:

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17134.0\um\x86
Run Code Online (Sandbox Code Playgroud)

您系统上的确切路径可能会因操作系统版本等而异,但我相信您明白了。找到位置后,您可以在CMakeLists.txt文件中添加编译器标志的路径,例如,

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xlinker /libpath:path_to_library")
Run Code Online (Sandbox Code Playgroud)

参见相关回答:

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

/sf/answers/3400337461/

标志如何/libpath使用:

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


Hol*_*Cat 1

如果我没记错的话,Clang 默认会尝试在 Windows 上使用 MSVC 的标准库,因为 Clang 自己的标准库还不能在 Windows 上运行。

如果您没有安装 MSVC,这会导致问题。

最简单的解决方案是安装 MSYS2 并使用 MSYS2 的修补 Clang,它默认使用 GCC 的库。作为一个不错的奖励,MSYS2 还附带了最新的 GCC 版本。


或者,您可以使用-targetflag 告诉 Clang 使用 GCC 的库。如果我没记错的话,这是通过添加-target x86_64-w64-mingw32编译器和链接器标志来完成的。

(如果不起作用,请尝试-target x86_64-w64-windows-gnu,我不记得是哪一个了。如果您使用的是 32 位编译器,请替换为 。x86_64i686

  • @Pedro那是因为OP的问题与CMake无关,如果我是正确的话。 (3认同)
  • @Peter 很高兴它起作用了。但不需要单独安装 MinGW,因为 MSYS2 安装了它自己的 MinGW GCC 作为 Clang 的依赖项。 (2认同)