【问题标题】:Cython dynamic library linkingCython 动态库链接
【发布时间】: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_ 不在符号中。

如果有人知道发生了什么、为什么会这样以及如何解决我的问题,这可能真的很有帮助!

【问题讨论】:

    标签: python dll cython


    【解决方案1】:

    我终于找到了解决方案,所以如果将来有人需要帮助,我会发布它!

    我在 Windows 上使用 Visual Studio 编译我的代码。

    即使我将 cfuncproject 创建为 Visual C++ 项目,它也没有编译为 C++ 项目,而是作为 C 项目编译,因此它默认工作(它只有一个 .c 和一个 .h 文件) .

    我的entrypoint.h 只包含 C 风格的函数声明,但 dll 被编译为 C++ 项目,这就是它无法工作的原因,名称修饰很糟糕。

    所以我刚刚在我的setup.py 中添加了language = 'c++'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 2015-08-25
      • 2010-09-15
      • 2015-07-10
      • 1970-01-01
      相关资源
      最近更新 更多