C++ C栋和x2B栋+;使用Cmake,使用Ninja作为生成器,使用cl clang作为编译器

C++ C栋和x2B栋+;使用Cmake,使用Ninja作为生成器,使用cl clang作为编译器,c++,windows,cmake,clang,cl,C++,Windows,Cmake,Clang,Cl,以前也有人问过类似的问题,但我在链接过程中遇到了一个问题,我还没有看到解决的问题,所以我在这里提出了一个新问题 我的目标是在Windows命令行上使用 ClClang< 编译一些C++代码作为编译器,我想使用忍者作为CMAGER生成器。我目前正在尝试编译一个简单的hello world程序。我的目录如下所示: . ├── CMakeLists.txt ├── setup.bat └── src └── main.cpp 为了简化设置,我使用以下setup.bat文件配置cmake: @

以前也有人问过类似的问题,但我在链接过程中遇到了一个问题,我还没有看到解决的问题,所以我在这里提出了一个新问题

我的目标是在Windows命令行上使用<代码> ClClang< <代码>编译一些C++代码作为编译器,我想使用忍者作为CMAGER生成器。我目前正在尝试编译一个简单的hello world程序。我的目录如下所示:

.
├── CMakeLists.txt
├── setup.bat
└── src
    └── main.cpp
为了简化设置,我使用以下
setup.bat
文件配置cmake:

@ECHO OFF
if not exist "build\" mkdir build\
cd build
call "C:\Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Auxiliary/Build/vcvarsall.bat" x86
set CC=clang-cl.exe
set CXX=clang-cl.exe
cmake -G Ninja ..
这个
CMakeLists.txt

cmake_minimum_required(VERSION 3.17.0)
project(ClangNinja)
enable_language(C CXX)

add_executable(foo_bar src/main.cpp)
cmake设置似乎工作正常,输出如下:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.8.3
** Copyright (c) 2020 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
-- The C compiler identification is Clang 10.0.0 with MSVC-like command-line
-- The CXX compiler identification is Clang 10.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/dev/c++/cmake/clang_cl_ninja/build
但是,当我尝试构建我的项目时,我得到了以下输出,其中有一些链接错误:

[2/2] Linking CXX executable foo_bar.exe
FAILED: foo_bar.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\foo_bar.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\mt.exe --manifests  -- C:\PRO
GRA~2\MICROS~3\2019\PROFES~1\VC\Tools\Llvm\bin\lld-link.exe /nologo CMakeFiles\foo_bar.dir\src\main.cpp.obj  /out:foo_bar.exe /implib:foo_bar.lib /pdb:foo_bar.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console  kernel32.
lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~2\MICROS~3\2019\PROFES~1\VC\Tools\Llvm\bin\lld-link.exe /nologo CMakeFiles\foo_bar.dir\src\main.cpp.obj /out:foo_bar.exe /implib:foo_bar.lib /pdb:foo_bar.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /s
ubsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\foo_bar.dir/intermediate.manifest CMakeFiles\foo_bar.dir/manifest.res
" failed (exit code 1) with the following output:
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 'msvcrtd.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 'msvcprtd.lib': no such file or directory
ninja: build stopped: subcommand failed.
据我所知,它在这里使用了所有正确的工具,因此我假设
vcvvarsall.bat
按预期工作,因为CMakeCache.txt包含以下内容:

CMAKE_C_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe
CMAKE_CXX_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/clang-cl.exe
CMAKE_LINKER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/bin/lld-link.exe

链接步骤中出现错误消息的原因是什么?

您是否介意显示CMakeLists.txt?很好,现在就这样做。还有一个愚蠢的问题-您真的在32位主机上运行此功能吗?如果不是,我会假设将arch更改为
x86_amd64
可能会有所不同?为了再现性,我还建议将
cmake--build
步骤添加到批处理文件中,以及在初始cmake调用之前设置
并包含完整的输出,以便我们也可以看到环境变量。