使用cliutic构建库

2024-05-19 19:29:24 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有人有一个很好的例子来使用distutils中的build_clib命令从setup.py?关于这个主题的文献似乎很少或根本不存在。在

我的目标是构建一个非常简单的外部库,然后构建一个连接到它的cython包装器。我发现的最简单的例子是here,但这使用了对gcc的system()调用,我无法想象这是最佳实践。在


Tags: py命令build目标主题heresetupclib
1条回答
网友
1楼 · 发布于 2024-05-19 19:29:24

传递包含要编译的源的元组,而不是将库名称作为字符串传递:

设置.py

import sys
from distutils.core import setup
from distutils.command.build_clib import build_clib
from distutils.extension import Extension
from Cython.Distutils import build_ext

libhello = ('hello', {'sources': ['hello.c']})

ext_modules=[
    Extension("demo", ["demo.pyx"])
]

def main():
    setup(
        name = 'demo',
        libraries = [libhello],
        cmdclass = {'build_clib': build_clib, 'build_ext': build_ext},
        ext_modules = ext_modules
    )

if __name__ == '__main__':
    main()

你好。c

^{pr2}$

你好。h

int hello(void);

演示.pyx

cimport demo
cpdef test():
    return hello()

演示.pxd

cdef extern from "hello.h":
    int hello()

代码作为主旨可用:https://gist.github.com/snorfalorpagus/2346f9a7074b432df959

相关问题 更多 >