gpt4 book ai didi

python - 使用 distutils 和 build_clib 构建 C 库

转载 作者:太空狗 更新时间:2023-10-29 16:56:37 26 4
gpt4 key购买 nike

有没有人有在 distutils 中使用 build_clib 命令从 setup.py 构建外部(非 python)C 库的好例子?有关该主题的文档似乎很少或根本不存在。

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

最佳答案

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

setup.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

int hello(void) { return 42; }

你好.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

关于python - 使用 distutils 和 build_clib 构建 C 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16854066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com