MSVC错误LNK2001:无法解析的外部符号

时间:2020-05-06 10:47:44

标签: c windows visual-studio visual-c++

我正在尝试将包含cython绑定的Python库编译为用C编写的FFTW库。我在Windows计算机上,并且已更新到最新的MSVC构建工具。我同时安装了Build Tools 2017(v15.9.22)和Build Tools 2019(v16.5.4)。

我使用tsne创建了一个名为conda create --name tsne python=3.7.4的单独的conda环境,并使用conda install -c conda-forge fftw使用conda安装了FFTW。我的setup.py定义了适当的扩展名和目录来编译模块。例如,在miniconda3\envs\tsne\Library\include中正确包含了C标头的include dirs,在miniconda3\envs\tsne\Library\lib中也正确包含了库目录。

当我尝试编译依赖FFTW的扩展时,出现以下错误

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.25.28610\bin\HostX86\x64\cl.exe
  /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\pavli\miniconda3\envs\tsne\include
  -IC:\Users\pavli\miniconda3\envs\tsne\Library\include
  -IC:\Users\pavli\miniconda3\envs\tsne\lib\site-packages\numpy\core\include
  -IC:\Users\pavli\miniconda3\envs\tsne\include
  -IC:\Users\pavli\miniconda3\envs\tsne\include
  "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.25.28610\include"
  "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt"
  "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared"
  "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um"
  "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt"
  "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt"
  /TcopenTSNE/_matrix_mul/matrix_mul_fftw3.c
  /Fobuild\temp.win-amd64-3.7\Release\openTSNE/_matrix_mul/matrix_mul_fftw3.obj  
  /Ox  /openmp
  matrix_mul_fftw3.c

C:\Users\pavli\miniconda3\envs\tsne\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(14) : Warning Msg: Using deprecated NumPy API, disable it with #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
openTSNE/_matrix_mul/matrix_mul_fftw3.c(2704): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(2741): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(2760): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(3199): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(3199): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(3237): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(3237): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(3257): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data
openTSNE/_matrix_mul/matrix_mul_fftw3.c(3257): warning C4244: 'function': conversion from 'Py_ssize_t' to 'int', possible loss of data

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.25.28610\bin\HostX86\x64\link.exe
  /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO 
  /LIBPATH:C:\Users\pavli\miniconda3\envs\tsne\lib 
  /LIBPATH:C:\Users\pavli\miniconda3\envs\tsne\Library\lib 
  /LIBPATH:C:\Users\pavli\miniconda3\envs\tsne\libs 
  /LIBPATH:C:\Users\pavli\miniconda3\envs\tsne\PCbuild\amd64
  "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.25.28610\lib\x64"
  "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64" 
  "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64" 
  /EXPORT:PyInit_matrix_mul build\temp.win-amd64-3.7\Release\openTSNE/_matrix_mul/matrix_mul_fftw3.obj 
  /OUT:C:\Users\pavli\Desktop\openTSNE\openTSNE\_matrix_mul\matrix_mul.cp37-win_amd64.pyd 
  /IMPLIB:build\temp.win-amd64-3.7\Release\openTSNE/_matrix_mul\matrix_mul.cp37-win_amd64.lib 
  /Ox  /openmp

LINK : warning LNK4044: unrecognized option '/Ox'; ignored
LINK : warning LNK4044: unrecognized option '/openmp'; ignored
   Creating library build\temp.win-amd64-3.7\Release\openTSNE/_matrix_mul\matrix_mul.cp37-win_amd64.lib and object build\temp.win-amd64-3.7\Release\openTSNE/_matrix_mul\matrix_mul.cp37-win_amd64.exp
matrix_mul_fftw3.obj : error LNK2001: unresolved external symbol fftw_destroy_plan
matrix_mul_fftw3.obj : error LNK2001: unresolved external symbol fftw_plan_dft_r2c_1d
matrix_mul_fftw3.obj : error LNK2001: unresolved external symbol fftw_execute
matrix_mul_fftw3.obj : error LNK2001: unresolved external symbol fftw_plan_dft_c2r_2d
matrix_mul_fftw3.obj : error LNK2001: unresolved external symbol fftw_plan_dft_r2c_2d
matrix_mul_fftw3.obj : error LNK2001: unresolved external symbol fftw_plan_dft_c2r_1d
C:\Users\pavli\Desktop\openTSNE\openTSNE\_matrix_mul\matrix_mul.cp37-win_amd64.pyd : fatal error LNK1120: 6 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.25.28610\\bin\\HostX86\\x64\\link.exe' failed with exit status 1120

该错误表明未找到编译的库(我猜吗?我不太熟悉...)。但是,我可以验证该库在那里。因此miniconda3\envs\tsne\Library\lib\fftw3.lib确实存在。然后,我认为fftw3.lib文件可能出了点问题,因此我使用dumpbin.exe转储了这些符号。链接器说的所有符号实际上都缺失了,例如fftw_execute

的相应转储块
  Version      : 0
  Machine      : 8664 (x64)
  TimeDateStamp: 5DC30B59 Wed Nov  6 19:05:13 2019
  SizeOfData   : 00000017
  DLL name     : fftw3.dll
  Symbol name  : fftw_execute
  Type         : code
  Name type    : name
  Hint         : 60
  Name         : fftw_execute

因此,如果我理解正确,那么lib文件就可以了。在Unix系统上,我可以通过指定-lfftw标志来告诉链接器该去哪里,但是显然,在MSVC中不存在。我很困惑,因为传递了链接器标志/LIBPATH:C:\Users\pavli\miniconda3\envs\tsne\Library\lib,但显然找不到fftw3.lib文件吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

根据评论,我没有在fftw3.lib的参数中指定link.exe/LIBPATH的行为类似于unix编译器上的-L标志,并且-l标志没有等效项。相反,我们必须传递完整的lib路径或仅传递lib文件名。