环境描述

  • window10
  • python3.6.8 [MSC v.1916 64 bit (AMD64)]
  • pytorch 1.10.0+cu113
  • cuda版本 11.1

使用mingw32编译

编译成目标文件通过,但是链接阶段发生问题,典型错误为

D:\ProgramData\mingw64\bin\g++.exe -shared -s build\temp.win-amd64-3.6\Release\mylinear.o build\temp.win-amd64-3.6\Release\mylinear_cpp.cp36-win_amd64.def -LD:\Python36WindowsTensorflow\Python36\lib\site-packages\torch\lib -LD:\Python36WindowsTensorflow\Python36\libs -LD:\Python36WindowsTensorflow\Python36\PCbuild\amd64 -lc10 -ltorch -ltorch_cpu -ltorch_python -lpython36 -lmsvcr120 -o build\lib.win-amd64-3.6\mylinear_cpp.cp36-win_amd64.pyd
build\temp.win-amd64-3.6\Release\mylinear.o:mylinear.cpp:(.text+0x160): undefined reference to `__imp__ZN2at4_ops13transpose_int4callERKNS_6TensorExx'
build\temp.win-amd64-3.6\Release\mylinear.o:mylinear.cpp:(.text+0x171): undefined reference to `__imp__ZN2at4_ops2mm4callERKNS_6TensorES4_'
build\temp.win-amd64-3.6\Release\mylinear.o:mylinear.cpp:(.text+0x18c): undefined reference to `__imp__ZN3c1019UndefinedTensorImpl10_singletonE'
...
...
build\temp.win-amd64-3.6\Release\mylinear.o:mylinear.cpp:(.text$_ZZN8pybind1112cpp_function10initializeIRPFSt6vectorIN2at6TensorESaIS4_EES4_S4_S4_ES6_JS4_S4_S4_EJNS_4nameENS_5scopeENS_7siblingEA18_cEEEvOT_PFT0_DpT1_EDpRKT2_ENUlRNS_6detail13function_callEE1_4_FUNESR_[_ZZN8pybind1112cpp_function10initializeIRPFSt6vectorIN2at6TensorESaIS4_EES4_S4_S4_ES6_JS4_S4_S4_EJNS_4nameENS_5scopeENS_7siblingEA18_cEEEvOT_PFT0_DpT1_EDpRKT2_ENUlRNS_6detail13function_callEE1_4_FUNESR_]+0x186): undefined reference to `__imp_THPVariableClass'
build\temp.win-amd64-3.6\Release\mylinear.o:mylinear.cpp:(.text$_ZZN8pybind1112cpp_function10initializeIRPFSt6vectorIN2at6TensorESaIS4_EES4_S4_S4_ES6_JS4_S4_S4_EJNS_4nameENS_5scopeENS_7siblingEA18_cEEEvOT_PFT0_DpT1_EDpRKT2_ENUlRNS_6detail13function_callEE1_4_FUNESR_[_ZZN8pybind1112cpp_function10initializeIRPFSt6vectorIN2at6TensorESaIS4_EES4_S4_S4_ES6_JS4_S4_S4_EJNS_4nameENS_5scopeENS_7siblingEA18_cEEEvOT_PFT0_DpT1_EDpRKT2_ENUlRNS_6detail13function_callEE1_4_FUNESR_]+0x228): undefined reference to `__imp__ZN3c106detail23torchInternalAssertFailEPKcS2_jS2_S2_'
build\temp.win-amd64-3.6\Release\mylinear.o:mylinear.cpp:(.text$_ZZN8pybind1112cpp_function10initializeIRPFSt6vectorIN2at6TensorESaIS4_EES4_S4_S4_ES6_JS4_S4_S4_EJNS_4nameENS_5scopeENS_7siblingEA18_cEEEvOT_PFT0_DpT1_EDpRKT2_ENUlRNS_6detail13function_callEE1_4_FUNESR_[_ZZN8pybind1112cpp_function10initializeIRPFSt6vectorIN2at6TensorESaIS4_EES4_S4_S4_ES6_JS4_S4_S4_EJNS_4nameENS_5scopeENS_7siblingEA18_cEEEvOT_PFT0_DpT1_EDpRKT2_ENUlRNS_6detail13function_callEE1_4_FUNESR_]+0x274): undefined reference to `__imp__ZN3c1019UndefinedTensorImpl10_singletonE'
collect2.exe: error: ld returned 1 exit status
error: command 'D:\\ProgramData\\mingw64\\bin\\g++.exe' failed with exit status 1

推测原因为,由于python和pytorch都是有msvc编译的出的静态库.lib和动态库.dll,如果采用g++链接,会由于不同编译器上对函数的编码规则,而无法找到相关的引用。这个在官方文档中也有提到,如下

A small note on compilers: Due to ABI versioning issues, the compiler you use to build your C++ extension must be ABI-compatible with the compiler PyTorch was built with. In practice, this means that you must use GCC version 4.9 and above on Linux. For Ubuntu 16.04 and other more-recent Linux distributions, this should be the default compiler already. On MacOS, you must use clang (which does not have any ABI versioning issues). In the worst case, you can build PyTorch from source with your compiler and then build the extension with that same compiler.

采用msvc编译

需要在python的安装目录D:\Python36WindowsTensorflow\Python36\Lib\distutils\disutils.cfg中作如下改变(如果没有这个文件,自行创建即可)

[build]
#compiler=mingw32
compiler=msvc[build_ext]
#compiler=mingw32
compiler=msvc

当然,可能需要升级msvc的版本。在使用vs2015的编译工具cl.exe,由于对c++14中常量表达式的支持不好,会遇到很多错误。升级到使用vs2017的工具包能完美解决。

D:\Program Files\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:D:\Python36WindowsTensorflow\Python36\lib\site-packages\torch\lib /LIBPATH:D:\Python36WindowsTensorflow\Python36\libs /LIBPATH:D:\Python36WindowsTensorflow\Python36\PCbuild\amd64 "/LIBPATH:D:\Program Files\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64" c10.lib torch.lib torch_cpu.lib torch_python.lib /EXPORT:PyInit_mylinear_cpp build\temp.win-amd64-3.6\Release\mylinear.obj /OUT:build\lib.win-amd64-3.6\mylinear_cpp.cp36-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.6\Release\mylinear_cpp.cp36-win_amd64.lib正在创建库 build\temp.win-amd64-3.6\Release\mylinear_cpp.cp36-win_amd64.lib 和对象 build\temp.win-amd64-3.6\Release\mylinear_cpp.cp36-win_amd64.exp
正在生成代码
已完成代码的生成

windows上编译安装pytorch的c++扩展相关推荐

  1. 在 OSX 10.9.4 上编译安装 FastDFS 及其 PHP 扩展

    2019独角兽企业重金招聘Python工程师标准>>> 经我测试,在OSX 10.9.4上安装FastDFS V5.01的话会很悲剧,一开始就无法安装,会遇到这帖子中的问题:http ...

  2. 安装flarum的php扩展,在 Windows 上快速安装 Flarum 指南

    在 Windows 上快速安装 Flarum 指南 由 学院君 创建于5年前, 最后更新于 10个月前 版本号 #2 17639 views 6 likes 0 collects 1.下载安装包 去下 ...

  3. php在windows编译_在Windows上编译PHP

    php在windows编译 Compiling PHP from source code is more commonly done on Unix-type systems. Those worki ...

  4. Windows上PostgreSQL安装配置教程

    这篇文章主要为大家详细介绍了Windows上PostgreSQL安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 PostgreSQL的扩展PostGIS是最著名的开源GIS数据库.  ...

  5. qt5.11 linux oracle,Qt5.11.1 + VS2017环境搭建(Qt5.11在windows上的安装)

    安装视频:<Qt5.11在windows'上的编译安装> 安装VisualStudio2017 VS2017安装包 提取码:7db1 1,安装vs2017 请确保勾选了"通用Wi ...

  6. CentOS 7上编译安装PHP 8.1及Nginx 配置支持PHP

    CentOS 7上编译安装PHP 8.1/及Nginx 配置支持PHP 编译安装 Php[端口:9000] 安装编译环境依赖包 yum -y install gcc gcc-c++ glibc aut ...

  7. 手把手教你用delphi 10.2.3 在windows上编译Centos 7下的命令行程序,适合中小学生

    手把手教你用delphi 10.2.3 在windows上编译Centos 7下的命令行程序,适合中小学生 1.下载并安装     a)下载:delphicbuilder10_2_3_2631.iso ...

  8. 在Windows上编译FreeRDP

    在Windows上编译FreeRDP FreeRDP是RDP协议的一个开源实现,像mstsc远程桌面也是RDP协议的一个实现微软把它预装在Windows系统中.今天我们就讲下如何在Windows下编译 ...

  9. 在 Windows 上手动安装 PHP

    在 Windows 上手动安装 PHP 选择 Web 服务器 阿帕奇 选择编译版本 选择 Web 服务器 IIS 是 Windows 内置的服务.在 Windows 服务器版本上,请使用服务器管理(S ...

最新文章

  1. python矩阵的平方_NumPy之计算两个矩阵的成对平方欧氏距离
  2. 重启服务器之home下文件全没,小白宝典——树莓派实用工具分享(大神绕路)
  3. 史上最全Redis面试题
  4. mysql 1418错误_mysql 1418错误原因及解决
  5. python3语法糖_python的语法糖
  6. 非泄露,NSA官方开源反汇编工具GHIDRA
  7. spark入门及环境搭建
  8. Lesson 04:类和对象,类的成员变量、成员方法、构造方法
  9. 数二用书推荐以及避雷
  10. Symbian手记【四】 —— Symbian的容器
  11. 怎么查看笔记本内存条型号_「电脑内存怎么看」电脑内存怎么看大小(内存条怎么看几代型号) - seo实验室...
  12. Android 自定义 View 实现转盘功能
  13. “名媛”不只值一个爱马仕包的钱
  14. 链表详解(单链表、双向链表、链表逆序)
  15. 最全Visual Studio版本号对应表VisualStudioVersion
  16. UP Meta—Web3.0世界创新型元宇宙金融协议
  17. python entry如何清空_如何清除tkinter中来自Entry小部件的以前输入?
  18. GeoAO:一种快速的环境光遮蔽方案
  19. Ubuntu下Intel A201 Wifi6驱动安装
  20. 电影数据分析练习——ppt报告

热门文章

  1. layui官方网站镜像地址
  2. 《计算机操作系统》课程学习(1)——第1章 操作系统引论
  3. OPNET仿真陈敏教程第6章报错解决方法
  4. 以流量计建立的远程抄表系统问题
  5. 支付宝h5支付(java版)
  6. 封装type方法,用于判断数组类型,解决typeof方法的不足之处
  7. 输入框回车多个文本_CAD制图初学入门:回车键和空格键在CAD软件中的作用
  8. 光模块第六节之VCSEL规格书解读
  9. 从 Masscan, Zmap 源码分析到开发实践
  10. 武汉光迅科技Java笔试题