_nms.pyd 编译出错问题记录

  python 编译扩展库 _nms.pyd(python build.py )时出现链接错误,如下:

  

  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THFloatTensor_data
  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THByteTensor_fill
  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THByteTensor_data
  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THByteTensor_free
  nms.obj : error LNK2001: 无法解析的外部符号 __imp__THArgCheck
  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THByteTensor_newWith
  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THLongTensor_isConti
  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THLongTensor_data
  nms.obj : error LNK2001: 无法解析的外部符号 __imp_THFloatTensor_size

 

  应该是没有链接上函数的实现,也就是动态库,利用depends软件,在TH.h文件附件文件夹找动态库,再根据网上搜索,发现TH.h里的函数实现在ATen.dll中

  

 

   然后就在 build.py中加上 ATen.dll对应的静态库,改代码如下:

  

import os
import torch
from torch.utils.ffi import create_extension
#from torch.utils.cpp_extension import BuildExtension

sources = ['src/nms.c']
headers = ['src/nms.h']
defines = []
with_cuda = False

# if torch.cuda.is_available():
# print('Including CUDA code.')
# sources += ['src/nms_cuda.c']
# headers += ['src/nms_cuda.h']
# defines += [('WITH_CUDA', None)]
# with_cuda = True

this_file = os.path.dirname(os.path.realpath(__file__))
print(this_file)
# extra_objects = ['src/cuda/nms_kernel.cu.o']
extra_objects = ['src/ATen.lib']
extra_objects = [os.path.join(this_file, fname) for fname in extra_objects]

ffi = create_extension(
#ffi = BuildExtension(
'_ext.nms',
headers=headers,
sources=sources,
define_macros=defines,
relative_to=__file__,
with_cuda=False,
extra_objects=extra_objects
)

if __name__ == '__main__':
ffi.build()

然后再编译就OK
posted on 2021-11-24 11:31  WenJXUST  阅读(317)  评论(0)    收藏  举报