You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows平台用MinGW编译LibTorch 2.0失败的解决咨询

问题

在Windows 11(Windows 7下同样存在)系统中使用PyTorch C++版本(LibTorch 2.0),按照官方指导搭建项目后,执行mingw32-make时出现编译错误,Linux环境下编译正常。

项目目录结构

example-app/
  build
  CMakeLists.txt
  example-app.cpp

CMake配置命令及输出

执行命令:

cmake .. -G "MinGW Makefiles" .

输出:

(base) PS D:\pytorch-cpp\pytorch-test\example-app\build> cmake .. -G "MinGW Makefiles" .      
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/MinGW/bin/gcc.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: D:/MinGW/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Torch: D:/pytorch-cpp/pytorch-cpu/libtorch/lib/torch.lib  
-- Pytorch status:
--     libraries: torch;torch_library;D:/pytorch-cpp/pytorch-cpu/libtorch/lib/c10.lib;D:/pytorch-cpp/pytorch-cpu/libtorch/lib/kineto.lib
-- Configuring done
-- Generating done
-- Build files have been written to: D:/pytorch-cpp/pytorch-test/example-app/build

编译错误信息

执行mingw32-make后报错:

(base) PS D:\pytorch-cpp\pytorch-test\example-app\build> mingw32-make
[ 50%] Building CXX object CMakeFiles/app.dir/example-app.cpp.obj
g++.exe: error: /EHsc: No such file or directory
g++.exe: error: /DNOMINMAX: No such file or directory
g++.exe: error: /wd4267: No such file or directory
g++.exe: error: /wd4251: No such file or directory
g++.exe: error: /wd4522: No such file or directory
g++.exe: error: /wd4838: No such file or directory
g++.exe: error: /wd4305: No such file or directory
g++.exe: error: /wd4244: No such file or directory
g++.exe: error: /wd4190: No such file or directory
g++.exe: error: /wd4101: No such file or directory
g++.exe: error: /wd4996: No such file or directory
g++.exe: error: /wd4275: No such file or directory
g++.exe: error: /bigobj: No such file or directory
CMakeFiles\app.dir\build.make:75: recipe for target 'CMakeFiles/app.dir/example-app.cpp.obj' failed
mingw32-make[2]: *** [CMakeFiles/app.dir/example-app.cpp.obj] Error 1
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/app.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/app.dir/all] Error 2
Makefile:89: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

项目代码

example-app.cpp内容:

#include <iostream>
#include <torch/torch.h>
int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}

CMakeLists.txt内容:

cmake_minimum_required(VERSION 3.0)
project(example-app)

list(APPEND CMAKE_PREFIX_PATH "D:/pytorch-cpp/pytorch-cpu/libtorch")
find_package(Torch REQUIRED)
if(NOT Torch_FOUND)
    message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status:")
message(STATUS "    libraries: ${TORCH_LIBRARIES}")

add_executable(app example-app.cpp)
target_link_libraries(app ${TORCH_LIBRARIES})
set_property(TARGET app PROPERTY CXX_STANDARD 11)
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET cpp
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:cpp>)
endif (MSVC)

解决方案

错误原因是LibTorch的CMake配置默认引入了MSVC编译器专属的编译选项(如/EHsc、/wdXXXX、/bigobj等),而MinGW的g++编译器无法识别这些参数,导致编译失败。需要修改CMakeLists.txt,移除这些不兼容的编译选项。

修改后的CMakeLists.txt

在find_package(Torch REQUIRED)之后,添加过滤MSVC编译选项的代码,同时修正原配置中的目标名错误:

cmake_minimum_required(VERSION 3.0)
project(example-app)

list(APPEND CMAKE_PREFIX_PATH "D:/pytorch-cpp/pytorch-cpu/libtorch")
find_package(Torch REQUIRED)
if(NOT Torch_FOUND)
    message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status:")
message(STATUS "    libraries: ${TORCH_LIBRARIES}")

# 移除MinGW不兼容的MSVC编译选项
if(NOT MSVC)
    string(REGEX REPLACE "/EHsc|/DNOMINMAX|/wd[0-9]+|/bigobj" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    string(REGEX REPLACE "/EHsc|/DNOMINMAX|/wd[0-9]+|/bigobj" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
    string(REGEX REPLACE "/EHsc|/DNOMINMAX|/wd[0-9]+|/bigobj" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
endif()

add_executable(app example-app.cpp)
target_link_libraries(app ${TORCH_LIBRARIES})
set_property(TARGET app PROPERTY CXX_STANDARD 11)
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:app>)
endif (MSVC)

重新编译步骤

  1. 进入build目录,删除原有编译文件:
rm -rf *
  1. 重新执行CMake配置:
cmake .. -G "MinGW Makefiles" .
  1. 执行编译:
mingw32-make

验证输出

编译完成后运行生成的app.exe,会得到如下输出:

0.2063  0.6593  0.0866
0.0796  0.5841  0.1569
[ Variable[CPUFloatType]{2,3} ]

内容的提问来源于stack exchange,提问作者agogag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:35:09