[英]Python CFFI: Build single module from multiple source files
My aim is to build a single module/extension from multiple C source files.我的目标是从多个 C 源文件构建单个模块/扩展。 What is the best practice to achieve this?
实现这一目标的最佳实践是什么?
What I tried:我尝试了什么:
I know that the ffi.set_source
function has the optional sources
and include_dir
kwargs and that there should be a possibility to use these kwargs to build a single shared object from multiple source files.我知道
ffi.set_source
function 具有可选的sources
和include_dir
kwargs,并且应该有可能使用这些 kwargs 从多个源文件构建单个共享 object。 Unfortunately I cannot find anything regarding these kwargs in the API reference of the CFFI Documentation .不幸的是,我在CFFI 文档的 API 参考资料中找不到任何关于这些 kwargs 的信息。 So I can't figure out the details.
所以我无法弄清楚细节。 Is there any document which explains how to properly use these kwargs?
是否有任何文件解释如何正确使用这些 kwargs?
I currently use:我目前使用:
SOURCES = ('file_1.c', 'file_2.c')
SRC_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../src')
ffi_builder = FFI()
ffi_builder.set_source(
'module_name._module_name',
''.join(f'#include "{source}"\n' for source in SOURCES),
include_dirs=[SRC_ROOT],
)
...
Although this actually works, it seems a bit hacky to me.虽然这确实有效,但对我来说似乎有点 hacky。 Is it possible to use
sources
kwarg instead of the 2nd positional arg source
?是否可以使用
sources
kwarg 而不是第二个位置 arg source
? This would allow me to get rid of the hacky ''.join(f'#include "{source}"\n' for source in SOURCES)
part.这将允许我摆脱
''.join(f'#include "{source}"\n' for source in SOURCES)
部分。
If there is even a simpler approach, I would also be interested!如果有更简单的方法,我也会感兴趣!
Any help is appreciated.任何帮助表示赞赏。 Thank you!
谢谢!
The documentation for set_source
states set_source
状态的文档
The keywords arguments to
set_source()
control how the C compiler will be called.关键字 arguments 到
set_source()
控制如何调用 C 编译器。 They are passed directly todistutils
orsetuptools
and include at leastsources
,include_dirs
,define_macros
,undef_macros
,libraries
,library_dirs
,extra_objects
,extra_compile_args
andextra_link_args
.它们直接传递给
distutils
或setuptools
并且至少包括sources
、include_dirs
、define_macros
、undef_macros
、libraries
、library_dirs
、extra_objects
、extra_compile_args
和extra_link_args
。 You typically need at leastlibraries=['foo']
in order to link withlibfoo.so
orlibfoo.so.XY
, orfoo.dll
on Windows.您通常至少需要
libraries=['foo']
才能与libfoo.so
或libfoo.so.XY
或foo.dll
上的 foo.dll 链接。 Thesources
is a list of extra.c files compiled and linked together (the filemodule_name.c
shown above is always generated and automatically added as the first argument tosources
).sources
是一个额外的列表。c 文件编译和链接在一起(上面显示的文件module_name.c
总是生成并作为第一个参数自动添加到sources
)。 See the distutils documentations for more information about the other arguments.有关其他 arguments 的更多信息,请参阅 distutils 文档。
So yes, you can use sources
as a kwarg所以是的,你可以使用
sources
作为一个 kwarg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.