有没有人有一个很好的例子,在setup.py中使用distutils中的build_clib命令来构建外部(非python)C库?关于这个主题的文档似乎很少或根本不存在。
我的目标是构建一个非常简单的外部库,然后构建一个链接到它的cython包装器。我找到的最简单的例子是here,但它使用了对gcc的system()调用,我无法想象这是最佳实践。
发布于 2013-06-01 17:12:00
不是将库名作为字符串传递,而是传递一个带有要编译的源代码的元组:
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()hello.c
int hello(void) { return 42; }hello.h
int hello(void);demo.pyx
cimport demo
cpdef test():
return hello()demo.pxd
cdef extern from "hello.h":
int hello()代码可以作为要点提供:https://gist.github.com/snorfalorpagus/2346f9a7074b432df959
https://stackoverflow.com/questions/16854066
复制相似问题