【发布时间】:2017-04-20 14:05:59
【问题描述】:
我实际上是在尝试将现有的 C 库链接到我的 Cython 程序。
我可以访问库的入口点标头 (.h),所有函数都声明为:
EXPORT_API int _stdcall LibFunction();
我想EXPORT_API 用于创建带有__declspec(dllexport) 的dll...
我还可以访问 .lib 和 .dll 文件。
我已尝试将此功能与通常的 cdef extern fromof Cython 一起使用:
cdef extern from "include\\entrypoint.h":
int LibFunction()
def c_LibFunction():
LibFunction()
我正在使用以下 setup.py
from setuptools import setup, Extension
from Cython.Distutils import build_ext
NAME = 'testlib'
REQUIRES = ['cython']
SRC_DIR = 'testlib'
PACKAGES = [SRC_DIR]
INCLUDE_DIR = 'testlib\include'
LIB_DIR = 'testlib\lib'
ext = Extension(SRC_DIR + '.wrapped',
[SRC_DIR + '/wrapped.pyx'],
include_dirs=[INCLUDE_DIR],
library_dirs = [LIB_DIR],
libraries=['cfunc', 'MyLib']
)
if __name__ == "__main__":
setup(
install_requires=REQUIRES,
packages=PACKAGES,
name=NAME,
ext_modules=[ext],
cmdclass={"build_ext": build_ext}
)
但是当我编译我的 Cython python setup.py build_ext 时,我得到一个未解析的外部引用:
error LNK2001: unresolved external symbol __imp_LibFunction
正如我在 other thread 上发现的,这似乎是静态或动态库链接的问题。
我认为它来自 setuptools 编译选项,我尝试使用 distutils documentation 和 Cython documentation 进行调查。
问题是,我还尝试制作自己的 C 库(cfunc.lib,一个静态库),并且我设法以与上述相同的方式在其中使用函数。
我还在 MyLib.lib 上使用了 DUMPBIN,发现符号 int __cdecl LibFunction(void),正如预期的那样,__imp_ 不在符号中。
如果有人知道发生了什么、为什么会这样以及如何解决我的问题,这可能真的很有帮助!
【问题讨论】: