环境描述

  • 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. cpc卡内计费信息异常包括_抖音CPC是什么?广告的优势在哪?抖音CPC广告转化率高吗?...
  2. mysql 查看当前连接数 和 最大连接数
  3. js在上传图片前判断大小
  4. vb的GUID生成算法
  5. BXUG第11期活动
  6. [css] 如何修改美化radio、checkbox的默认样式?
  7. Javaoop_继承
  8. IT也要健康:帮助你保持健康的几个重要因素
  9. 跟着AlphaGo 理解深度强化学习框架
  10. vue el-date-picker 直接赋值不生效、数据绑定无效
  11. unix linux性能对比,各有所长!对比LINUX和UNIX系统优势
  12. 元气骑士机器人旁边建筑_元气骑士机器人的一些经验
  13. php矢量绘图,打算做一个开源的矢量绘图软件
  14. Android自定义锁屏实现----仿正点闹钟滑屏解锁
  15. 破解IntelliJ IDEA 2017.1到2099年
  16. 强化学习PARL——5. 基于连续动作空间上方法求解RL及大作业
  17. Go2Shell 打开设置窗口
  18. python编程控制机器人_python程序控制NAO机器人行走
  19. 【集成学习(上)】机器学习基础_02
  20. (附源码)springboot基于微信小程序的高校计算机类课程思政库的设计与实现 毕业设计 271611

热门文章

  1. 职高学计算机可以考的大学名单,职高学生可以考的大学名单 职高和普高有什么区别...
  2. Android版酷狗音乐 v9.0.1 去广告SVIP珍藏V2版
  3. PlayWright自动化测试工具
  4. java后端面试不知道多少家重庆的公司得来的题目总结
  5. 北京第九次Spark meetup会议资料分享
  6. ablation study解释
  7. VM下的ubuntu连不上网的解决办法
  8. LabWindows CVI 2017开发笔记--串口调试软件实例
  9. O2O对消费者有什么好处 O2O模式的消费者特征是怎么样的?
  10. 数据分析师,到底要懂多少业务?