【发布时间】:2021-04-13 15:29:55
【问题描述】:
我对 CMake 非常陌生,并尝试在已安装 Visual Studio 17 和 19 的 Windows 上构建一个最小的 CMake 文件。C++ 代码是最低限度的 main.cpp:
#include<iostream>
int main()
{
std::cout << "Hello" << std::endl;
return 0;
}
CMakeLists.txt文件也是最小的:
cmake_minimum_required(VERSION 3.1)
project(hello)
add_executable(${PROJECT_NAME} main.cpp)
当我在命令行上运行 CMake 时,我得到了这个错误,我不知道如何解决它:
C:\SampleProject\build>cmake ./../src
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
Failed to run MSBuild command:
MSBuild.exe
to get the value of VCTargetsPath:
The system cannot find the file specified
【问题讨论】:
-
键入 msbuild.exe 而不是键入
cmake ./../src会给您一个“msbuild.exe 未被识别为内部或外部命令” -
您必须从“x64 Native Tools Command Prompt for VS 2019”运行
cmake(这是 Visual Studio 在开始菜单中安装的快捷方式)。也可以试试 Ninja,它使用 CMake 比使用 MSBuild 更容易。 -
感谢您的回答。我无法在命令行上运行 MSBuild,当我为 VS 2017 运行 x64 本机工具时,它工作了!
-
我几乎总是从 Visual Studio 命令提示符运行 CMake,并设置路径和环境变量。虽然对我来说,这部分是由于安装了多个编译器和工作环境的多个构建树。