使用distutils和build_clib构建C库 [英] Using distutils and build_clib to build C library
问题描述
有人在distutils中使用 build_clib
命令从setup.py构建外部(非python)C库吗?
我的目标是建立一个非常简单的外部库,然后建立一个链接到它的cython包装器。我找到的最简单的示例是此处,但这使用了
system()
调用。 div> 不是将库名作为字符串传递,而是将元组与源一起传递来编译:
setup.py
import sys
从distutils.core导入设置
从distutils.command.build_clib import build_clib
来自distutils.extension import扩展
来自Cython.Distutils import build_ext
libhello =('hello',{'sources':['hello.c']})
ext_modules = [
Extension( demo,[ demo.pyx])
]
def main():
setup(
名称='演示',
库= [libhello],
cmdclass = {'build_clib':build_clib,'build_ext':build_ext},
ext_modules = ext_modules
)
如果__name__ == '__main__':
main()
hello.c
int hello(void){返回42; }
hello.h
int hello(void);
demo.pyx
导入演示
cpdef test():
return hello()
demo.pxd
cdef extern来自你好.h:
int hello()
代码大致可用: https://gist.github.com/snorfalorpagus/2346f9a7074b432df959
Does anyone have a good example of using the build_clib
command in distutils to build an external (non-python) C library from setup.py? The documentation on the subject seems to be sparse or non-existent.
My aim is to build a very simple external library, then build a cython wrapper which links to it. The simplest example I've found is here, but this uses a system()
call to gcc which I can't imagine is best practice.
Instead of passing a library name as a string, pass a tuple with the sources to compile:
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()
Code is available as a gist: https://gist.github.com/snorfalorpagus/2346f9a7074b432df959
这篇关于使用distutils和build_clib构建C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!