首页 > 解决方案 > CMake 和 Visual Studio:无法运行 MSBuild.exe

问题描述

我正在尝试构建 OpenCV 的源文件并使用 CMake 生成一个 Visual Studio 项目。我面临的问题似乎无法通过这些给出的以下建议解决:此处- 使用 CMake 构建时选择 Visual Studio 的正确版本(例如 v16 2019),安装MSBuild.exe并将其位置添加到系统 PATH -,此处- 安装 Windows SDK -在这里- 在 Visual Studio 中安装有关 CMake/C++ 所需的工作负载并重新启动它。

然后我请求你的帮助。我目前正在与:

  1. 操作系统:Windows 8.1 Pro 64 位
  2. CMake (GUI) 版本 3.17.2,位于C:\Program Files\CMake\bin
  3. Microsoft Visual Studio 2019 版本 16.6.0
  4. 微软构建。exe 版本 16.6.0,位于C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin
  5. OpenCV 4.3.0,位于D:\opencv
  6. sdksetup.exe版本 8.100,位于C:\Program Files (x86)\Microsoft SDKs\Windows Kits\8.1\StandaloneSDK
  7. 我的系统环境变量中的路径是:%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\MinGW\bin;C:\Program Files\CMake\bin\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\;C:\Program Files (x86)\Microsoft SDKs\Windows Kits\8.1\Windows Performance Toolkit;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\

我以管理员权限打开 CMake,然后选择“Visual Studio 16 2019”作为生成器,单击“配置”按钮时总是弹出以下错误:

配置过程出错,项目文件可能无效。

我试着用两种不同的方法来做CMakeLists.txt,一个简单的,一个完整的。

简单的脚本

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Define project name
project(opencv_example_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})

在这种情况下,状态栏警告:

CMake Error at CMakeLists.txt:5 (project):
  Failed to run MSBuild command:

    C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe

  to get the value of VCTargetsPath:

    Microsoft(R) Build Engine versÆo 16.6.0+5ff7b0c9e para .NET Framework 

    Copyright (C) Microsoft Corporation. Todos os direitos reservados.

完整的脚本

可以在这里找到https://github.com/opencv/opencv/blob/master/CMakeLists.txt。我小心地删除了所有 CMakeCache 文件并删除了 CMake-GUI 中的缓存。在这种情况下,状态栏警告:

CMake Error at CMakeLists.txt:106 (enable_language):
  Failed to run MSBuild command:

    C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe

  to get the value of VCTargetsPath:

    Microsoft(R) Build Engine versÆo 16.6.0+5ff7b0c9e para .NET Framework 

刚刚结束:在 Microsoft Visual Studio 的“关于”部分,我们可以看到加载了哪些工作负载:

非常感谢!

标签: windowsvisual-studiovisual-c++cmakemsbuild

解决方案


推荐阅读