为什么 PyTorch 无法使用 MinGW 构建?

2024-01-06

我尝试在win11上使用C++版本的Pytorch(版本2.0)即LibTorch,但是当我执行mingw32-make操作时,出现丢失文件错误,如下所示:

示例目录的布局如下:

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

运行详细输出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

(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)

我尝试了多种方法都编译成功,都报同样的错误,甚至使用Win7和Linux,其中Win7与Win11有相同的错误,但在Linux上编译良好。在Windows系统上应该如何解决这个问题?

程序的正确结果应该是:

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

在撰写本文时,不支持使用 MinGW 构建 PyTorch。您可以改用 Visual Studio CMake 生成器。

See https://github.com/pytorch/pytorch/issues/24460#issuecomment-1383277152 https://github.com/pytorch/pytorch/issues/24460#issuecomment-1383277152.

注意:我实际上无法分辨why特别是您添加了这些标志。我搜索了 PyTorch 存储库,发现 PyTorch 存储库中并未提及您在目标上获得的所有标志。有些是(参见https://github.com/pytorch/pytorch/blob/main/CMakeLists.txt#L894 https://github.com/pytorch/pytorch/blob/main/CMakeLists.txt#L894),但他们在那里指定的方式(CMAKE_<LANG>_FLAGS https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html)不应传播到任何 PyTorch 目标的链接相关目标,它们甚至在控制流块中指定,仅在满足条件时才输入MSVC https://cmake.org/cmake/help/latest/variable/MSVC.html是真的,这是写它在那里做什么的正确方法(错误的方法是使用WIN32 https://cmake.org/cmake/help/latest/variable/WIN32.html)。 ˙\_(ツ)_/˙

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 PyTorch 无法使用 MinGW 构建? 的相关文章

  • C# 创建函数队列

    我写了一个名为 QueueManager 的类 class QueueManager Queue functionsQueue public bool IsEmpty get if functionsQueue Count 0 return
  • 起订量要求?违背了目的?

    是否需要虚拟化您想要模拟的所有属性访问器就违背了模拟的目的 我的意思是 如果我必须修改我的对象并虚拟化我想要模拟的每个访问器 我难道不能继承我的类并自己模拟它吗 你的问题非常有效 但如果你仔细想想 没有其他方法可以模拟课程 如果你采用一个接
  • SharpZipLib - 将文件夹/目录添加到 zip 存档

    通过示例 我很好地掌握了如何提取 zip 文件 几乎在每个示例中 识别 ZipEntry 是否为目录的方法如下 string directoryName Path GetDirectoryName theEntry Name string
  • 将图像文件从网址复制到本地文件夹?

    我有该图像的网址 例如 http testsite com web abc jpg http testsite com web abc jpg 我想将该 URL 复制到 c images 中的本地文件夹中 而且当我将该文件复制到文件夹中时
  • 键盘加速器在 UWP 应用中停止工作

    我正在尝试将键盘加速器添加到 UWP 应用程序中的 CommandBar 菜单项 当应用程序启动时 这工作正常 但在我第一次打开溢出菜单后 加速器停止工作 这似乎不会发生在主要命令 菜单之外 上 只有溢出菜单内的辅助命令才会发生 此外 单击
  • 使用 OpenGL 着色器进行数学计算 (C++)

    我有一个矩阵 例如 100x100 尺寸 我需要对每个元素进行计算 matrix i j tt 8 5例如 我有一个巨大的矩阵 我想使用 OpenGL 着色器来实现该算法 我想使用着色器 例如 uniform float val unifo
  • 如何在编译C代码时禁用警告?

    我正在使用 32 位 Fedora 14 系统 我正在使用编译我的源代码gcc 有谁知道如何在编译c代码时禁用警告 EDIT 是的 我知道 最好的办法是修复这些警告以避免任何未定义 未知的行为 但目前在这里 我第一次编写了巨大的代码 并且在
  • 矩阵向量变换

    我正在编写一个代码来制作软件蒙皮器 骨骼 皮肤动画 并且我正处于 优化 阶段 蒙皮器工作得很好 并且在 Core 上 1 09 毫秒内对 4900 个三角形网格与 22 个骨骼进行蒙皮Duo 2 Ghz 笔记本 我需要知道的是 1 有人可以
  • 是否有像 gccxml 这样的用于生成包装器的 C 标头解析器工具?

    我需要为一种新的编程语言编写一些 C 标头包装器 并且想要类似 gccxml 的东西 但不完全依赖 gcc 以及它在 Windows 系统上带来的问题 只需要读C而不是C 只要有完整的文档记录 任何格式的输出都可以 Linux Solari
  • 更改 IdentityServer4 实体框架表名称

    我正在尝试更改由 IdentityServer4 的 PersistedGrantDb 和 ConfigurationDb 创建的默认表名称 并让实体框架生成正确的 SQL 例如 而不是使用实体IdentityServer4 EntityF
  • 系统错误 124 - SHFileOperation 的 ERROR_INVALID_LEVEL

    我在使用时遇到问题SHFileOperation SHFileOperation SHFILEOPSTRUCT https stackoverflow com questions 9191415 shfileoperation shfile
  • 正确使用“extern”关键字

    有一些来源 书籍 在线材料 解释了extern如下 extern int i declaration has extern int i 1 definition specified by the absence of extern 并且有支
  • 设计 Javascript 前端 <-> C++ 后端通信

    在我最近的将来 我将不得不制作一个具有 C 后端和 Web 前端的系统 要求 目前 我对此了解不多 我认为前端将触发数据传输 而不是后端 所以不需要类似 Comet 的东西 由于在该领域的经验可能很少 我非常感谢您对我所做的设计决策的评论
  • 相当于 C# 中 Java 的“ByteBuffer.putType()”

    我正在尝试通过从 Java 移植代码来格式化 C 中的字节数组 在 Java 中 使用方法 buf putInt value buf putShort buf putDouble 等等 但我不知道如何将其移植到 C 我尝试过 MemoryS
  • 如何访问窗口?

    我正在尝试使用其句柄访问特定窗口 即System IntPtr value Getting the process of Visual Studio program var process Process GetProcessesByNam
  • “int i=1,2,3”和“int i=(1,2,3)”之间的区别 - 使用逗号运算符的变量声明[重复]

    这个问题在这里已经有答案了 int i 1 2 3 int i 1 2 3 int i i 1 2 3 这些说法有什么区别 我无法找出任何具体原因 Statement 1 Result Compile error 运算符的优先级高于 运算符
  • 使用 Chrome 和 Selenium 设置 LocalStorage

    我正在尝试使用 OpenQA Selenium 和 Chrome 设置本地存储键和值 我认为这相当微不足道 但我似乎无法让它发挥作用 我对 C 很陌生 所以我可能错过了一些东西 无论如何 我有这个功能 public static void
  • 从有符号字符转换为无符号字符然后再转换回来?

    我正在使用 JNI 并有一个 jbyte 类型的数组 其中 jbyte 表示为有符号字符 即范围从 128 到 127 jbyte 表示图像像素 对于图像处理 我们通常希望像素分量的范围为0到255 因此 我想将jbyte值转换为0到255
  • 如何设置 CMake 与 clang 交叉编译 Windows 上的 ARM 嵌入式系统?

    我正在尝试生成 Ninja makefile 以使用 Clang 为 ARM Cortex A5 CPU 交叉编译 C 项目 我为 CMake 创建了一个工具链文件 但似乎存在错误或缺少一些我无法找到的东西 当使用下面的工具链文件调用 CM
  • FindAsync 很慢,但是延迟加载很快

    在我的代码中 我曾经使用加载相关实体await FindAsync 希望我能更好地遵守 C 异步指南 var activeTemplate await exec DbContext FormTemplates FindAsync exec

随机推荐