gpt4 book ai didi

python CFFI : Build single module from multiple source files

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:53 24 4
gpt4 key购买 nike

我的目标是从多个 C 源文件构建单个模块/扩展。实现这一目标的最佳做法是什么?

我尝试了什么:

我知道 ffi.set_source 函数有可选的 sourcesinclude_dir kwargs 并且应该有可能使用这些 kwargs从多个源文件构建一个共享对象。不幸的是,我在 CFFI Documentation 的 API 引用中找不到关于这些 kwargs 的任何信息。 .所以我无法弄清楚细节。是否有任何文档解释如何正确使用这些 kwargs?

我目前使用:

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],
)

...

虽然这确实有效,但对我来说似乎有点老套。是否可以使用 sources kwarg 而不是第二个位置参数 source?这将使我摆脱 hacky ''.join(f'#include "{source}"\n' for source in SOURCES) 部分。

如果有更简单的方法,我也会感兴趣!

感谢任何帮助。谢谢!

最佳答案

documentation for set_source

The keywords arguments to set_source() control how the C compiler will be called. They are passed directly to distutils or setuptools and include at least sources, include_dirs, define_macros, undef_macros, libraries, library_dirs, extra_objects, extra_compile_args and extra_link_args. You typically need at least libraries=['foo'] in order to link with libfoo.so or libfoo.so.X.Y, or foo.dll on Windows. The sources is a list of extra .c files compiled and linked together (the file module_name.c shown above is always generated and automatically added as the first argument to sources). See the distutils documentations for more information about the other arguments.

所以是的,您可以使用 sources 作为 kwarg

关于 python CFFI : Build single module from multiple source files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58108580/

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