为啥 cl.exe(Visual Studio 编译器)无法使用 CMake(错误报告)编译项目?

Posted

技术标签:

【中文标题】为啥 cl.exe(Visual Studio 编译器)无法使用 CMake(错误报告)编译项目?【英文标题】:How come cl.exe (Visual Studio compiler) fails to compile a project with CMake (error-report)?为什么 cl.exe(Visual Studio 编译器)无法使用 CMake(错误报告)编译项目? 【发布时间】:2019-10-23 04:11:06 【问题描述】:

由于我是 CMake 新手,我一直在尝试使用 CMake GUI 设置基本的 CMake 应用程序,同时关注 this tutorial。在我单击“配置”之前一切正常。我将 Visual Studio 14 2015 更改为 Visual Studio 16 2019(因为我有更新版本的 Visual Studio)和“Hello World”更改为“Basic”,单击“配置”后,我收到一条错误消息,告诉我 Visual Studio 编译器, cl.exe, 无法成功编译测试程序。更多详情如下。

我已经使用 Visual Studio 的内置工作空间创建了一个 CMake 项目,它已经成功编译了我的项目;但是,这对我来说是不可取的,因为 Visual Studio 向我的项目中添加了一堆文件,这使得它对于初学者来说看起来更复杂并且弄脏了我的项目。这告诉我我的 Visual Studio 安装没有缺陷,并且 CMake 有问题。

对于那些想知道的人,我的 CMakeLists.txt 看起来像这样:

cmake_minimum_required(VERSION "3.10")

project("Basic")

add_executable("$PROJECT_NAME" "main.cpp")

install(TARGETS "$PROJECT_NAME" DESTINATION bin)
install(FILES "main.cpp" DESTINATION src)

我希望 CMake 能够像教程视频中的那个人一样正确配置我的项目,但是我在配置时收到错误消息(出于个人原因,用户名替换为“potato”):

Selecting Windows SDK version 10.0.18362.0 to target Windows 6.1.7601.
The C compiler identification is MSVC 19.23.28106.4
The CXX compiler identification is MSVC 19.23.28106.4
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:60 (message):
  The C compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/potato/Desktop/Basic/build/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe cmTC_09d22.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=16.0 /v:m && Microsoft (R) Build Engine version 16.3.1+1def00d3d for .NET Framework

    Copyright (C) Microsoft Corporation. All rights reserved.



    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(5589,7): error MSB4023: Cannot evaluate the item metadata "%(FullPath)". The item metadata "%(FullPath)" cannot be applied to the path "@(_DebugSymbolsIntermediatePath->'C:\Users\potato\Desktop\Basic\build\CMakeFiles\CMakeTmp\Debug\%(Filename)%(Extension)')". Illegal characters in path. [C:\Users\potato\Desktop\Basic\build\CMakeFiles\CMakeTmp\cmTC_09d22.vcxproj]






  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)


Configuring incomplete, errors occurred!
See also "C:/Users/potato/Desktop/Basic/build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/potato/Desktop/Basic/build/CMakeFiles/CMakeError.log".

CMakeError.log 声明如下:

Determining if the C compiler works failed with the following output:
Change Dir: C:/Users/potato/Desktop/Basic/build/CMakeFiles/CMakeTmp

Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe cmTC_09d22.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=16.0 /v:m && Microsoft (R) Build Engine version 16.3.1+1def00d3d for .NET Framework

Copyright (C) Microsoft Corporation. All rights reserved.



C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(5589,7): error MSB4023: Cannot evaluate the item metadata "%(FullPath)". The item metadata "%(FullPath)" cannot be applied to the path "@(_DebugSymbolsIntermediatePath->'C:\Users\potato\Desktop\Basic\build\CMakeFiles\CMakeTmp\Debug\%(Filename)%(Extension)')". Illegal characters in path. [C:\Users\potato\Desktop\Basic\build\CMakeFiles\CMakeTmp\cmTC_09d22.vcxproj]

也许我还应该注意,由“potato”代替的真实用户名包含一个撇号。这可能只是最小的,但它可能很重要,因为我阅读了错误“路径中的非法字符”。

【问题讨论】:

创建一个c:\test 文件夹,将项目放在那里,然后重试。如果这不起作用,您至少可以消除撇号。 是的,我应该想到这一点。它确实有效。我会在这里发布答案。 【参考方案1】:

事实证明,我的问题是我的计算机的用户名包含撇号。绝对是一个奇怪的错误。

【讨论】:

CMake ducomentation 表示它支持 unicode,但也:请注意,由于 CMake 与许多其他工具互操作,因此在使用某些 unicode 字符时可能仍然存在一些限制。 您能否包括您为解决这个问题而经历的演绎或调试步骤?这是一个很大的飞跃。

以上是关于为啥 cl.exe(Visual Studio 编译器)无法使用 CMake(错误报告)编译项目?的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 社区 2017 cl.exe

Visual Studio 2017 找不到 cl.exe

Visual Studio 2015 没有 cl.exe

如何为 Visual Studio 构建工具 cl.exe 编写 makefile(nmake)

Windows 7 64 位 Visual Studio 2013 上的本机 cl.exe

为 cl.exe (Visual Studio Code) 指定命令行 C++ 版本