使用CMake和clang在Windows上构建一个简单的C ++项目 [英] Building a simple C++ project on Windows, using CMake and clang

查看:307
本文介绍了使用CMake和clang在Windows上构建一个简单的C ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得一个简单的"Hello World"程序以在Windows 10上构建,最好使用CMake和clang.如果我使用MinGW的g ++编译器,则可以成功地编译,链接和运行相同的项目,但是在尝试使用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++.

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

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

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

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

现在,当我使用简单的"Hello World" C ++项目运行cmake时,cmake抱怨无法使用clang:

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

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

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.

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

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?

推荐答案

如果我没记错的话,Clang默认会尝试在Windows上使用MSVC的标准库,因为Clang自己的标准库在Windows上尚不可用.

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.

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

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

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

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.

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

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.

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

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

这篇关于使用CMake和clang在Windows上构建一个简单的C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆