背景故事: 对于我需要大量计算的项目之一,我目前正在使用我的Python代码调用来自C ++代码的.exe代码,该代码在磁盘上读/写文件,因此非常慢。
为了解决这个问题,我在我的Python代码中重新编写了用作DLL的C ++代码,因为那时不会有磁盘访问,它应该更快。为此,我计划使用Cython。
问题: 为了更好地理解它是如何工作的,我因此this tutorial跟J.J. Hakala关注Cython实际上经常发帖的人。{/ p>
我跟踪了每个部分,我在Windows上的同一目录中:
complexFunLib.dll
complexFunLib.lib
complexFunLib.h
ccomplexFunLib.pxd
(是的,还有额外的' c')complexFunLib.pyx
setup.py
当我尝试首先运行python setup.py --build_ext --inplace
时,它无法找到库,所以我不得不使用另一个SO问题中的that trick来判断DLL的位置,所以{{1}我修改了:setup.py
然后该错误消失了。
这让我想到了当前未解决的错误:
当我运行libraries=[r'.\complexFunLib']
时,它使用MinGW并引发此错误:
编辑,我把完整的日志记录下来:
运行build_ext
cythoning complexFunLib.pyx到complexFunLib.cpp
building' complexFunLib'扩展
创建构建
创建build \ temp.win-amd64-3.6
创建build \ temp.win-amd64-3.6 \ Release
D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ Library \ mingw-w64 \ bin \ gcc.exe -mdll -O -Wall -DMS_WIN64 -ID:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ core \ include -ID:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ include-ID:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ include -c complexFunLib.cpp -o build \ temp .win-amd64-3.6 \发布\ complexfunlib.o 在D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ core \ include / numpy / ndarraytypes.h:1809:0中包含的文件中 从D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ core \ include / numpy / ndarrayobject.h:18, 来自D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ core \ include / numpy / arrayobject.h:4, 来自complexFunLib.cpp:500:
D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ core \ include / numpy / npy_1_7_deprecated_api.h:13:79:注意:#pragma message:
D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ core \ include / numpy / npy_1_7_deprecated_api.h(12):警告消息:使用已弃用的NumPy API,通过#defining禁用它NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
" #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION")
python setup.py --build_ext --inplace
complexFunLib.cpp:在函数' PyTypeObject * __Pyx_ImportType(const char *,const char *,size_t,int)':
complexFunLib.cpp:7149:53:警告:未知的转换类型字符' z'格式为[-Wformat =]
^
complexFunLib.cpp:7149:53:警告:未知的转换类型字符' z'格式为[-Wformat =]
complexFunLib.cpp:7149:53:警告:格式参数太多[-Wformat-extra-args]
编写build \ temp.win-amd64-3.6 \ Release \ complexFunLib.cp36-win_amd64.def
D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ Library \ mingw-w64 \ bin \ g ++。exe -shared -s build \ temp.win-amd64-3.6 \ Release \ complexfunlib.o build \ temp。 win-amd64-3.6 \ Release \ complexFunLib.cp36-win_amd64.def -L。 -LD:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ libs -LD:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ PCbuild \ amd64。\ complexFunLib.dll -lpython36 -lmsvcr140 -o" D:\ MYNAME \ PATH_TO_PROJECT \ complexFunLib.cp36-win_amd64.pyd"
建立\ temp.win-amd64-3.6 \推出\ complexfunlib.o:complexFunLib.cpp :(文本+ 0x29b5): 未定义的引用`mp_mlt_exp_c4'
建立\ temp.win-amd64-3.6 \推出\ complexfunlib.o:complexFunLib.cpp :(文本+ 0x3ba7): 对'mp_mlt_exp_c8'
的未定义引用collect2.exe:错误:ld返回1退出状态错误:命令' D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ Library \ mingw-w64 \ bin \ g ++。exe'退出状态1失败
当我跑(试试以防万一) module_name, class_name, basicsize, size);
^
因为我不知道它是否应该使用编译DLL的相同编译器编译,它使用Visual Studio' s编译器并引发此错误:
编辑,我为MSVC提供了完整的日志:
运行build_ext
cythoning complexFunLib.pyx到complexFunLib.cpp
building' complexFunLib'扩展
创建构建
创建build \ temp.win-amd64-3.6
创建build \ temp.win-amd64-3.6 \ Release
C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ VC \ BIN \ x86_amd64 \ cl.exe / c / nologo / Ox / W3 / GL / DNDEBUG / MD -ID:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ core \ include -ID:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ include -ID:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ include" - IC:\ ProgramFiles(x86)\ Microsoft Visual Studio 14.0 \ VC \ INCLUDE" " -IC:\ Program Files(x86)\ Windows Kits \ 10 \ include \ 10.0.10240.0 \ ucrt" " -IC:\ Program Files(x86)\ Windows Kits \ NETFXSDK \ 4.6.1 \ include \ um" " -IC:\ Program Files(x86)\ Windows Kits \ 8.1 \ include \ shared" " -IC:\ Program Files(x86)\ Windows Kits \ 8.1 \ include \ um" " -IC:\ Program Files(x86)\ Windows Kits \ 8.1 \ include \ winrt" / EHsc /TpcomplexFunLib.cpp /Fobuild\temp.win-amd64-3.6\Release\complexFunLib.obj
complexFunLib.cpp
d:\ MYNAME \ appdata \ local \ continuum \ anaconda3 \ lib \ site-packages \ numpy \ core \ include \ numpy \ npy_1_7_deprecated_api.h(12):警告消息:使用弃用的NumPy API,通过#defining禁用它NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ VC \ BIN \ x86_amd64 \ link.exe / nologo / INCREMENTAL:NO / LTCG / DLL / MANIFEST:EMBED,ID = 2 / MANIFESTUAC:NO / LIBPATH :。 / LIBPATH:D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ libs / LIBPATH:D:\ MYNAME \ AppData \ Local \ Continuum \ Anaconda3 \ PCbuild \ amd64" / LIBPATH:C:\ Program Files(x86 )\ Microsoft Visual Studio 14.0 \ VC \ LIB \ amd64" " / LIBPATH:C:\ Program Files(x86)\ Windows Kits \ 10 \ lib \ 10.0.10240.0 \ ucrt \ x64" " / LIBPATH:C:\ Program Files(x86)\ Windows Kits \ NETFXSDK \ 4.6.1 \ lib \ um \ x64" " / LIBPATH:C:\ Program Files(x86)\ Windows Kits \ 8.1 \ lib \ winv6.3 \ um \ x64" 。\ complexFunLib.lib / EXPORT:PyInit_complexFunLib build \ temp.win-amd64-3.6 \ Release \ complexFunLib.obj" / OUT:D:\ MYNAME \ PATH_TO_PROJECT \ complexFunLib.cp36-win_amd64.pyd" /IMPLIB:build\temp.win-amd64-3.6\Release\complexFunLib.cp36-win_amd64.lib complexFunLib.obj:警告LNK4197:export' PyInit_complexFunLib'指定多次;使用第一个规范
创建库build \ temp.win-amd64-3.6 \ Release \ complexFunLib.cp36-win_amd64.lib和object build \ temp.win-amd64-3.6 \ Release \ complexFunLib.cp36-win_amd64.exp
complexFunLib.obj:错误LNK2001:未解析的外部符号mp_mlt_exp_c8
complexFunLib.obj:错误LNK2001:未解析的外部符号mp_mlt_exp_c4
D:\ PATH_TO_PROJECT \ complexFunLib.cp36-win_amd64.pyd:致命错误LNK1120:2个未解析的外部
错误:命令' C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ VC \ BIN \ x86_amd64 \ link.exe'退出状态失败1120
我相信这两个错误是相同的,并且未定义的参考文献'或者'外部符号' python setup.py --build_ext --inplace --compiler=msvc
和mp_mlt_exp_c4
是在C ++标头和.cpp中声明和定义的函数,也是.pxd和.pyx中的函数,可以从我在开头链接的教程页面中看到。
我真的不确定如何解决这个问题,它看起来像是Cython机器的链接问题,所以如果知识渊博的人有解决方案,我很高兴听到它:)。< / p>
编辑:
@ead问的mp_mlt_exp_c8
:
转储文件complexFunLib.dll
文件类型:DLL
部分包含complexFunLib.dll的以下导出
dumpbin
摘要
00000000 characteristics 5B1800BA time date stamp Wed Jun 6 17:41:46 2018 0.00 version 1 ordinal base 2 number of functions 2 number of names ordinal hint RVA name 1 0 00001000 mp_mlt_exp_c4 = _mp_mlt_exp_c4 2 1 000010E0 mp_mlt_exp_c8 = _mp_mlt_exp_c8