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

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

我已经安装了CMake,MinGW和LLVM,并且可以在我的路径中访问它们:

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

我为CMake设置了使用clang的环境变量:

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

现在,当我使用简单的“ 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

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

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

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

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

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

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

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

查看相关答案:

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

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

如何使用/libpath标志:

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

,

要强制Clang使用其自己的库而不是MSVC,请将“ -target x86_64-w64-mingw32”添加到CMAKE_C(XX)_FLAGS。

当心:您必须在CMake识别编译器之前执行此操作,例如在CMake中第一个C或C ++项目定义之前(例如,在project()调用之前):

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

或者,您可以在命令行上将其与“ -D”传递给CMake。

经过clang 10.0测试

,

您的Clang编译器可能是针对MSVC ABI构建的。如果我尝试您的情况,这是我的错误消息:

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

这是与CMake 3.13和LLVM 9.0一起使用的,并尝试使用-G“ MinGW Makefiles”。它的工作原理如下:

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

您可能不需要安装Visual Studio,但是如果您要使用nmake和MSVC库,则肯定需要下载Windows 10 SDK,这是一个很大的下载,如果您也要安装它,决定安装Visual Studio。

另一方面,问题似乎与CMake 3.16版本有关。以上未成功的测试是使用cmake 3.13进行的,但此后我已升级到CMake 3.15.5,它可以完美地工作。这是确切的版本:

> 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

我的build.cmd脚本:

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

因此,问题可能出在您的CMake版本或MinGW库中,如错误消息所提示。

,

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

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

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


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

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

本文链接:https://www.f2er.com/3086556.html

大家都在问