【问题标题】:Python Struggling to create an C extension wrapping a 3rd party dllPython 努力创建一个包装第 3 方 dll 的 C 扩展
【发布时间】:2014-03-10 13:45:01
【问题描述】:
我们正在尝试包装第 3 方 dll(用 C 编写)以通过 python 访问它。
dll 有一个 .lib .c 和一个 .h 文件。我们通过 .c 文件访问 dll。
在扩展程序之外(作为控制台应用程序运行),代码可以正常工作。没有第 3 方 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'])]
)
################################################# ##
我们收到的错误是:
C:\Python33\source\Python-3.3.4\ChemAppPython>setup.py 安装
运行安装
运行构建
运行 build_ext
构建“chemAppPython”扩展
创建构建
创建 build\temp.win-amd64-3.3
创建 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) : 警告 C4267: 'function' : 从 'size_t' 转换为 'long',可能丢失数据
cacint.c(390) : 警告 C4267: 'function' : 从 'size_t' 转换为 'long', 可能丢失数据
.
.
. (针对不同功能的更多相同警告消息。)
.
cacint.c(619) : 警告 C4996: 'strcpy': 此函数或变量可能不安全。考虑改用 strcpy_s。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。
.
.
.
. (代码中不同位置的更多相同警告消息。)
.
创建 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:警告 LNK4197:多次指定导出“PyInit_chemAppPython”;使用第一个规范
创建库 build\temp.win-amd64-3.3\Release\chemAppPython.lib 和对象 build\temp.win-amd64-3.3\Release\chemAppPython.exp
cacint.obj:错误 LNK2019:函数 tqini 中引用的未解析的外部符号 TQINI
cacint.obj:错误 LNK2019:函数 tqopen 中引用的未解析的外部符号 TQOPEN
.
.
. (其中更多,用于不同的方法。同样,它在控制台应用程序主机应用程序中构建和运行良好。)
.
build\lib.win-amd64-3.3\chemAppPython.pyd : 致命错误 LNK1120: 74 unresolved externals
错误:命令 '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\link.exe"' 失败,退出状态为 1120
- 我按照 python 网站上的适用于 Windows 的 Python 扩展教程进行操作。
- 我可以从 Visual Studio 10.0 成功构建扩展,并使扩展从 Python 的源代码构建运行。
- 我无法通过已安装的 python(不是源代码构建)使其工作 我将创建的 .pyd 文件复制到 site-package 文件夹,当我尝试从 python 控制台导入扩展时收到错误消息。
【问题讨论】:
标签:
c
python-extensions