繁体   English   中英

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

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

我正在尝试在 Windows 10 上构建一个简单的“Hello World”程序,最好使用 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 抱怨无法使用 Z2C1517DB7BC39AE574F

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 无关。

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

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

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。

Beware: you have to do this before CMake identifies the compiler, eg either before the first C or C++ project definition in CMake (eg 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 ...)

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

使用 clang 10.0 测试

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

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

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


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

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

您的 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 库上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM