简体   繁体   English

Python CFFI:从多个源文件构建单个模块

[英]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 具有可选的sourcesinclude_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 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 .它们直接传递给distutilssetuptools并且至少包括sourcesinclude_dirsdefine_macrosundef_macroslibrarieslibrary_dirsextra_objectsextra_compile_argsextra_link_args You typically need at least libraries=['foo'] in order to link with libfoo.so or libfoo.so.XY , or foo.dll on Windows.您通常至少需要libraries=['foo']才能与libfoo.solibfoo.so.XYfoo.dll上的 foo.dll 链接。 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 ). 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.

相关问题 是否可以从 python 3.0 中的单个模块登录多个日志文件 - Is it possible to log into multiple log files from a single module in python 3.0 从 AWS Lambda 层导入 cffi python 模块时出错 - Error importing a cffi python module from AWS Lambda Layer 没有来自 FFI 的名为 CFFI 的模块 - No module named CFFI from FFI python 从源代码构建:无法构建可选模块 sqlite3 - python build from source: cannot build optional module sqlite3 在Python中从数据源创建多个文件 - Creating Multiple files from a data source in Python 使用 CFFI 构建 Python-C 扩展,但 Setuptools 在构建中不包含自定义头文件 - Building Python-C Extension using CFFI, but Setuptools does not include custom header files in build Python使用Setuptools在API模式下构建CFFI - Python build CFFI in API mode with Setuptools 从python中的单个文件生成多个文件 - Produce multiple files from a single file in python 将多个文件作为单个模块导入? - Importing multiple files as a single module? 使用 CFFI 在 Fortran 中使用 Python 函数。 cffi 构建中的警告。 我无法通过控制台或文件中的打印获得结果,但在运行时不会出错 - Use CFFI for using Python function inside Fortran. Warnings in cffi build. I can't get results from print in console or file, but not error in running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM