Python苦苦挣扎,创建一个包含第三方dll的C扩展

时间:2014-03-10 13:45:01

标签: c python-extensions

我们正在尝试包装第三方dll(用C语言编写)以通过python访问它。 dll有一个.lib .c和一个.h文件。我们通过.c文件访问dll。

扩展的扩展(作为控制台应用程序运行),代码可以正常运行。没有第三方DLL,python扩展工作没有问题。当尝试将第三方dll与python扩展组合时,会出现此问题。

这是distutil安装脚本

########## Setup.py ################################
from distutils.core import setup, Extension 

chemAppPython_mod = Extension('chemAppPython', sources = ['chemAppPython.c', 'cacint.c'], libraries=['ca_vc_opt_e'], depends = ['cacint.h']) 

setup(name = "chemAppPython", 
    version = "1.0", 
    description = "The ChemnApp Python module", 
    ext_modules = [chemAppPython_mod], 
        data_files = [('',['ca_vc_e.dll'])] 
) 
################################################## ##
  • ca_vc_opt_e.lib和ca_vc_e.dll是包含我们想要访问的第三方方法的库。

  • cacint.h和cacint.c是充当ca_vc_opt_e.lib和ca_vc_e.dll接口的文件。

  • chemAppPython.c是包含调用cacint.c(实际上是第三方dll)的代码的文件,将C代码暴露给Python。
The errors we are receiving are:
C:\Python33\source\Python-3.3.4\ChemAppPython>setup.py install
running install 
running build 
running build_ext 
building 'chemAppPython' extension 
creating build 
creating build\temp.win-amd64-3.3 
creating build\temp.win-amd64-3.3\Release 
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Python33\include -IC:\Python33\include /TcchemAppPython.c /Fobuild\temp.win-amd64-3.3\Release\chemAppPython.obj 
chemAppPython.c 
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Python33\include -IC:\Python33\include /Tccacint.c /Fobuild\temp.win-amd64-3.3\Release\cacint.obj 
cacint.c 
cacint.c(357) : warning C4267: 'function' : conversion from 'size_t' to 'long', possible loss of data 
cacint.c(390) : warning C4267: 'function' : conversion from 'size_t' to 'long', possible loss of data 
. 
. 
. (some more of the same warning message for different functions.) 
. 
cacint.c(619) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 
. 
. 
. 
. (some more of the same warning message at different positions in code.) 
. 
creating build\lib.win-amd64-3.3 
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Python33\libs /LIBPATH:C:\Python33\PCbuild\amd64 ca_vc_opt_e.lib /EXPORT:PyInit_chemAppPython build\temp.win-amd64-3.3\Release\chemAppPython.obj build\temp.win-amd64-3.3\Rele 
chemAppPython.obj : warning LNK4197: export 'PyInit_chemAppPython' specified multiple times; using first specification 
   Creating library build\temp.win-amd64-3.3\Release\chemAppPython.lib and object build\temp.win-amd64-3.3\Release\chemAppPython.exp 
cacint.obj : error LNK2019: unresolved external symbol TQINI referenced in function tqini 
cacint.obj : error LNK2019: unresolved external symbol TQOPEN referenced in function tqopen 
. 
. 
. (a lot more of them, for different methods. Again, it builds and runs fine in the console app host application.) 
. 
build\lib.win-amd64-3.3\chemAppPython.pyd : fatal error LNK1120: 74 unresolved externals 
error: command '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\link.exe"' failed with exit status 1120
  • 我从python网站上关注了Windows的Python扩展教程。
  • 我可以从Visual Studio 10.0成功构建扩展,并使用Python的源代码构建扩展。
  • 我无法通过已安装的python(不是源代码构建)使其工作我将创建的.pyd文件复制到site-package文件夹,并在我尝试从python控制台导入扩展时收到错误。

1 个答案:

答案 0 :(得分:0)

我解决了。显然,64位Python与32位dll没有很好的混合(或根本没有)。我将python降级为32位版本,一切正常。