在 msys2@mingw 下编译 BVLC/Caffe

本文介绍了在Windows上构建Caffe开发环境的步骤,包括所需工具安装、CMake配置以及遇到的常见错误如OpenCV版本兼容性、链接错误等的解决方法。

(参考 https://github.com/lemonsqueeze/mingw-caffe ,有关这个项目的尝试写在文章后面)

1.  准备开发环境

有关需要安装的工具,上面这篇已经说的很明白了,照搬于下。

pacman -S --needed git make patch diffutils
pacman -S --needed \
		mingw-w64-${MSYSTEM_CARCH}-cmake \
		mingw-w64-${MSYSTEM_CARCH}-python \
		mingw-w64-${MSYSTEM_CARCH}-tools-git \
		mingw-w64-${MSYSTEM_CARCH}-gcc \
		mingw-w64-${MSYSTEM_CARCH}-boost \
		mingw-w64-${MSYSTEM_CARCH}-protobuf-c \
		mingw-w64-${MSYSTEM_CARCH}-gflags \
		mingw-w64-${MSYSTEM_CARCH}-glog \
		mingw-w64-${MSYSTEM_CARCH}-hdf5 \
		mingw-w64-${MSYSTEM_CARCH}-openblas \
		mingw-w64-${MSYSTEM_CARCH}-leveldb \
		mingw-w64-${MSYSTEM_CARCH}-lmdb \
		mingw-w64-${MSYSTEM_CARCH}-snappy \
		mingw-w64-${MSYSTEM_CARCH}-python-matplotlib \
		mingw-w64-${MSYSTEM_CARCH}-python-pytest \
		mingw-w64-${MSYSTEM_CARCH}-python-scipy

 还要下载 caffe 源代码

wget https://github.com/BVLC/caffe/archive/refs/tags/1.0.tar.gz
tar xf 1.0.tar.gz
cd caffe

2.  编译脚本

#!/bin/bash
 
BUILD_DIR=${1:-build}
export  PATH=/mingw64/bin:/mingw64/include:$PATH
 
[ -e ${BUILD_DIR} ] && rm -r ${BUILD_DIR}/* || mkdir ${BUILD_DIR} 
 
cmake \
	-B${BUILD_DIR} \
	-DBLAS=open \
	caffe-1.0
 
# Build
[ $? -eq 0 ] &&  cmake --build ${BUILD_DIR} -j 8

3.  错误修复 

Error 1:

/home/rd/NN/caffe/src/caffe/layers/window_data_layer.cpp: In member function ‘virtual void caffe::WindowDataLayer<Dtype>::load_batch(caffe::Batch<Dtype>*)’:
/home/rd/NN/caffe/src/caffe/layers/window_data_layer.cpp:293:42: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
  293 |         cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR);
      |                 

PowerShell 7 环境已加载 (版本: 7.5.2) PS C:\Users\Administrator\Desktop> cd E:\PyTorch_Build\pytorch PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 删除损坏的虚拟环境 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Remove-Item rtx5070_env -Recurse -Force -ErrorAction SilentlyContinue (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 创建新环境 (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 激活环境 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\Activate.ps1 (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 安装必要依赖 (rtx5070_env) PS E:\PyTorch_Build\pytorch> pip install numpy ninja pybind11 Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting numpy Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a3/dd/4b822569d6b96c39d1215dbae0582fd99954dcbcf0c1a13c61783feaca3f/numpy-2.2.6-cp310-cp310-win_amd64.whl (12.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.9/12.9 MB 54.4 MB/s eta 0:00:00 Collecting ninja Downloading https://pypi.tuna.tsinghua.edu.cn/packages/29/45/c0adfbfb0b5895aa18cec400c535b4f7ff3e52536e0403602fc1a23f7de9/ninja-1.13.0-py3-none-win_amd64.whl (309 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 310.0/310.0 kB ? eta 0:00:00 Collecting pybind11 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/cd/8a/37362fc2b949d5f733a8b0f2ff51ba423914cabefe69f1d1b6aab710f5fe/pybind11-3.0.1-py3-none-any.whl (293 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 293.6/293.6 kB ? eta 0:00:00 Installing collected packages: pybind11, numpy, ninja Successfully installed ninja-1.13.0 numpy-2.2.6 pybind11-3.0.1 [notice] A new release of pip available: 22.3.1 -> 25.2 [notice] To update, run: python.exe -m pip install --upgrade pip (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 设置环境变量 (rtx5070_env) PS E:\PyTorch_Build\pytorch> $env:CUDA_PATH = "E:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0" (rtx5070_env) PS E:\PyTorch_Build\pytorch> $env:Path = "$env:CUDA_PATH\bin;$env:Path" (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 修复虚拟环境 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\fix_venv.ps1 .\fix_venv.ps1: The term '.\fix_venv.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 配置构建参数 (rtx5070_env) PS E:\PyTorch_Build\pytorch> $cmake_args = @( >> "-GNinja", >> "-DBUILD_PYTHON=ON", >> "-DUSE_CUDA=ON", >> "-DUSE_CUDNN=ON", >> "-DCUDNN_INCLUDE_DIR=E:\Program Files\NVIDIA\CUNND\v9.12\include\13.0", >> "-DCUDNN_LIBRARY=E:\Program Files\NVIDIA\CUNND\v9.12\lib\13.0\x64\cudnn64_9.lib", >> "-DTORCH_CUDA_ARCH_LIST=8.9", >> "-DCMAKE_BUILD_TYPE=Release" >> ) -join " " (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 配置构建 (rtx5070_env) PS E:\PyTorch_Build\pytorch> mkdir build -Force Directory: E:\PyTorch_Build\pytorch Mode LastWriteTime Length Name ---- ------------- ------ ---- da--- 2025/9/3 15:52 build (rtx5070_env) PS E:\PyTorch_Build\pytorch> cd build (rtx5070_env) PS E:\PyTorch_Build\pytorch\build> cmake .. $cmake_args CMake Error: Could not create named generator Ninja -DBUILD_PYTHON=ON -DUSE_CUDA=ON -DUSE_CUDNN=ON -DCUDNN_INCLUDE_DIR=E:\Program Files\NVIDIA\CUNND\v9.12\include\13.0 -DCUDNN_LIBRARY=E:\Program Files\NVIDIA\CUNND\v9.12\lib\13.0\x64\cudnn64_9.lib -DTORCH_CUDA_ARCH_LIST=8.9 -DCMAKE_BUILD_TYPE=Release Generators * Visual Studio 17 2022 = Generates Visual Studio 2022 project files. Use -A option to specify architecture. Visual Studio 16 2019 = Generates Visual Studio 2019 project files. Use -A option to specify architecture. Visual Studio 15 2017 = Generates Visual Studio 2017 project files. Use -A option to specify architecture. Visual Studio 14 2015 = Generates Visual Studio 2015 project files. Use -A option to specify architecture. Borland Makefiles = Generates Borland makefiles. NMake Makefiles = Generates NMake makefiles. NMake Makefiles JOM = Generates JOM makefiles. MSYS Makefiles = Generates MSYS makefiles. MinGW Makefiles = Generates a make file for use with mingw32-make. Green Hills MULTI = Generates Green Hills MULTI files (experimental, work-in-progress). Unix Makefiles = Generates standard UNIX makefiles. Ninja = Generates build.ninja files. Ninja Multi-Config = Generates build-<Config>.ninja files. Watcom WMake = Generates Watcom WMake makefiles. CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files (deprecated). CodeBlocks - NMake Makefiles = Generates CodeBlocks project files (deprecated). CodeBlocks - NMake Makefiles JOM = Generates CodeBlocks project files (deprecated). CodeBlocks - Ninja = Generates CodeBlocks project files (deprecated). CodeBlocks - Unix Makefiles = Generates CodeBlocks project files (deprecated). CodeLite - MinGW Makefiles = Generates CodeLite project files (deprecated). CodeLite - NMake Makefiles = Generates CodeLite project files (deprecated). CodeLite - Ninja = Generates CodeLite project files (deprecated). CodeLite - Unix Makefiles = Generates CodeLite project files (deprecated). Eclipse CDT4 - NMake Makefiles = Generates Eclipse CDT 4.0 project files (deprecated). Eclipse CDT4 - MinGW Makefiles = Generates Eclipse CDT 4.0 project files (deprecated). Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files (deprecated). Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files (deprecated). Kate - MinGW Makefiles = Generates Kate project files (deprecated). Kate - NMake Makefiles = Generates Kate project files (deprecated). Kate - Ninja = Generates Kate project files (deprecated). Kate - Ninja Multi-Config = Generates Kate project files (deprecated). Kate - Unix Makefiles = Generates Kate project files (deprecated). Sublime Text 2 - MinGW Makefiles = Generates Sublime Text 2 project files (deprecated). Sublime Text 2 - NMake Makefiles = Generates Sublime Text 2 project files (deprecated). Sublime Text 2 - Ninja = Generates Sublime Text 2 project files (deprecated). Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files (deprecated). (rtx5070_env) PS E:\PyTorch_Build\pytorch\build> (rtx5070_env) PS E:\PyTorch_Build\pytorch\build> # 开始构建 (rtx5070_env) PS E:\PyTorch_Build\pytorch\build> ninja install [0/1] Re-running CMake... FAILED: [code=1] build.ninja E:/PyTorch_Build/pytorch/build/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/protobuf/cmake/cmake_install.cmake E:/PyTorch_Build/pytorch/build/confu-deps/pthreadpool/cmake_install.cmake E:/PyTorch_Build/pytorch/build/FXdiv/cmake_install.cmake E:/PyTorch_Build/pytorch/build/confu-deps/cpuinfo/cmake_install.cmake E:/PyTorch_Build/pytorch/build/confu-deps/XNNPACK/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/googletest/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/googletest/googlemock/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/googletest/googletest/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/benchmark/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/benchmark/src/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ittapi/cmake_install.cmake E:/PyTorch_Build/pytorch/build/confu-deps/FP16/cmake_install.cmake E:/PyTorch_Build/pytorch/build/confu-deps/psimd/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/onnx/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/common/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/cpu/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/cpu/x64/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/interface/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/fake/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/dnnl/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/graph_compiler/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/utils/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/examples/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/tests/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/fmt/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/kineto/libkineto/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/kineto/libkineto/third_party/dynolog/dynolog/src/ipcfabric/cmake_install.cmake E:/PyTorch_Build/pytorch/build/third_party/mimalloc/cmake_install.cmake E:/PyTorch_Build/pytorch/build/torch/headeronly/cmake_install.cmake E:/PyTorch_Build/pytorch/build/c10/cmake_install.cmake E:/PyTorch_Build/pytorch/build/c10/test/cmake_install.cmake E:/PyTorch_Build/pytorch/build/c10/benchmark/cmake_install.cmake E:/PyTorch_Build/pytorch/build/c10/cuda/cmake_install.cmake E:/PyTorch_Build/pytorch/build/c10/cuda/test/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/THC/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/quantized/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/nnapi/cmake_install.cmake E:/PyTorch_Build/pytorch/build/sleef/cmake_install.cmake E:/PyTorch_Build/pytorch/build/sleef/src/cmake_install.cmake E:/PyTorch_Build/pytorch/build/sleef/src/libm/cmake_install.cmake E:/PyTorch_Build/pytorch/build/sleef/src/common/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/test/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/core/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/serialize/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/utils/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/perfkernels/cmake_install.cmake E:/PyTorch_Build/pytorch/build/test_jit/cmake_install.cmake E:/PyTorch_Build/pytorch/build/test_nativert/cmake_install.cmake E:/PyTorch_Build/pytorch/build/test_inductor/cmake_install.cmake E:/PyTorch_Build/pytorch/build/test_api/cmake_install.cmake E:/PyTorch_Build/pytorch/build/test_lazy/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/torch/cmake_install.cmake E:/PyTorch_Build/pytorch/build/caffe2/torch/lib/libshm_windows/cmake_install.cmake E:/PyTorch_Build/pytorch/build/functorch/cmake_install.cmake E:/PyTorch_Build/pytorch/build/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/protobuf/cmake/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/confu-deps/pthreadpool/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/FXdiv/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/confu-deps/cpuinfo/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/confu-deps/XNNPACK/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/googletest/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/googletest/googlemock/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/googletest/googletest/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/benchmark/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/benchmark/src/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ittapi/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/confu-deps/FP16/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/confu-deps/psimd/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/onnx/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/common/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/cpu/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/cpu/x64/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/interface/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/fake/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/dnnl/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/backend/graph_compiler/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/src/graph/utils/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/examples/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/ideep/mkl-dnn/tests/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/fmt/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/kineto/libkineto/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/kineto/libkineto/third_party/dynolog/dynolog/src/ipcfabric/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/third_party/mimalloc/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/torch/headeronly/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/c10/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/c10/test/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/c10/benchmark/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/c10/cuda/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/c10/cuda/test/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/THC/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/quantized/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/nnapi/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/sleef/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/sleef/src/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/sleef/src/libm/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/sleef/src/common/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/aten/src/ATen/test/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/core/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/serialize/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/utils/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/perfkernels/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/test_jit/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/test_nativert/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/test_inductor/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/test_api/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/test_lazy/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/torch/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/caffe2/torch/lib/libshm_windows/CTestTestfile.cmake E:/PyTorch_Build/pytorch/build/functorch/CTestTestfile.cmake E:\PyTorch_Build\pytorch\rtx5070_env\Lib\site-packages\cmake\data\bin\cmake.exe --regenerate-during-build -SE:\PyTorch_Build\pytorch -BE:\PyTorch_Build\pytorch\build CreateProcess failed: The system cannot find the file specified. ninja: error: rebuilding 'build.ninja': subcommand failed (rtx5070_env) PS E:\PyTorch_Build\pytorch\build> (rtx5070_env) PS E:\PyTorch_Build\pytorch\build> # 验证安装 (rtx5070_env) PS E:\PyTorch_Build\pytorch\build> cd .. (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import torch; print(f'PyTorch版本: {torch.__version__}, CUDA可用: {torch.cuda.is_available()}')" Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\PyTorch_Build\pytorch\torch\__init__.py", line 35, in <module> from typing_extensions import ParamSpec as _ParamSpec, TypeIs as _TypeIs ModuleNotFoundError: No module named 'typing_extensions' (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 授予执行权限 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-ExecutionPolicy Bypass -Scope Process -Force (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 运行完整构建 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\build_pytorch.ps1 .\build_pytorch.ps1: The term '.\build_pytorch.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch>
09-04
### ✅ 回答问题:`D:/msys2/msys64/mingw64/bin/mingw64.exe` 无法识别为命令 你说: > `D:/msys2/msys64/mingw64/bin/mingw64.exe : 无法将“...”项识别为 cmdlet、函数、脚本文件或可运行程序的名称` 这说明你在 **Windows 的 PowerShell 或 CMD** 中尝试直接运行 `mingw64.exe`,但系统找不到该文件或路径错误。 --- ### 🔍 问题产生的根本原因 #### ❌ 错误认知: 你认为 `mingw64.exe` 是一个独立可执行程序,位于: ``` D:/msys2/msys64/mingw64/bin/mingw64.exe ``` 但实际上: > 🚫 **`mingw64.exe` 并不存在于 `bin` 目录下!** 这个文件 **根本就不是编译器本身**,也不是一个 shell 可执行程序。这是对 MSYS2 架构的误解。 --- ### ✅ 正确认知:MSYS2 的启动器结构 MSYS2 安装后提供几个快捷方式来启动不同子系统的终端,例如: | 快捷方式 | 实际作用 | |--------|---------| | `MSYS2 UCRT64` | 启动 `ucrt64.exe`,并进入 UCRT 工具链环境 | | `MSYS2 MINGW64` | 启动 `mingw64.exe`,并进入 MinGW-w64 工具链环境 | | `MSYS2 CLANG64` | 启动 `clang64.exe` | 这些 `.exe` 文件(如 `mingw64.exe`)其实位于: ``` D:\msys2\mingw64.exe ← 根目录下,不是 bin 下! ``` 而不是: ``` ❌ D:\msys2\msys64\mingw64\bin\mingw64.exe → 这个路径完全错误 ``` 📌 所以你的命令失败的根本原因是: > ⛔ 路径写错了!`mingw64.exe` 不在 `bin` 文件夹里,也不在 `msys64` 子目录中。 --- ### ✅ 正确路径和使用方式 #### ✅ 1. 确认 `mingw64.exe` 是否存在 打开资源管理器,前往: ``` D:\msys2\ ``` 你应该看到以下文件(取决于你安装了哪些组件): - `msys2.exe` - `ucrt64.exe` - `mingw64.exe` ✅ - `clang64.exe` 👉 这些才是真正的“终端启动器”。 它们的作用是: - 启动一个带有正确环境变量(PATH、HOME 等)的 shell - 自动切换到工具链对应的 MinGW 环境 --- #### ✅ 2. 如何从外部调用它并执行命令? 如果你想在 VS Code 或 CMD 中通过 `mingw64.exe` 来运行 `gcc`,必须这样写: ```json { "command": "D:/msys2/mingw64.exe", "args": [ "-c", "gcc -g main.c -o main.exe" ], "options": { "cwd": "${workspaceFolder}" } } ``` 📌 注意: - `command` 是 `D:/msys2/mingw64.exe`(根目录下的启动器) - 使用 `-c` 参数传递要执行的命令(类似 Linux 的 `sh -c`) 这样就会启动 MinGW64 shell,并在其中运行 `gcc`,环境完整,DLL 路径也正确。 --- ### ✅ 验证方法:在 CMD 中测试是否能运行 打开 CMD,输入: ```cmd D:\msys2\mingw64.exe -c "gcc --version" ``` ✅ 如果配置正确,你会看到输出: ```text gcc (MinGW-W64 x86_64) 13.2.0 ... ``` ❌ 如果仍然报错找不到文件,请检查: 1. 是否真的存在 `D:\msys2\mingw64.exe` 2. 是否拼写错误(比如多了一个 `\msys64\`) 3. 是否以管理员身份运行?某些安全策略可能阻止执行 --- ### ✅ 常见错误对比表 | 错误路径 | 正确路径 | 说明 | |--------|--------|------| | `D:/msys2/msys64/mingw64/bin/mingw64.exe` | ❌ 不存在 | 把路径层级搞混了 | | `D:/msys2/mingw64/bin/gcc.exe` | ✅ 存在(如果是 MINGW64 安装) | 编译器真实位置 | | `D:/msys2/mingw64.exe` | ✅ 存在 | 终端启动器,用于运行命令 | --- ### ✅ 总结一句话: > `mingw64.exe` 是 MSYS2 提供的一个 **终端启动器**,位于 `D:\msys2\mingw64.exe`,而不是在 `bin` 目录下。你不能在 CMD 中直接引用一个不存在的路径。正确的做法是使用根目录下的 `mingw64.exe -c "gcc ..."` 来启动完整的 MinGW 环境进行编译。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值