给定源代码,如何使用 CFFI 调用现有的 C 函数?
How can use CFFI to call an existing C function given the source code?
我有一个 C source/header 文件,它是一个更大项目的一部分。我想将其作为一个独立于实际项目的单元进行测试。虽然可以通过创建一个具有不同 main()
的新项目在 C 中做到这一点,但我想看看我是否可以使用 Python (3) 及其框架(例如 nose)来加速测试的构建,使用现有的报告框架等
我的印象是我可以用 CFFI 做到这一点。这是一个示例 C 文件:
// magic.c
// Implementation of magic.
int add(int a, int b)
{
return a;
}
header:
// magic.h
// Add two numbers (where a + b is not greater than INT_MAX).
int add(int a, int b);
这是一个试图编译它的脚本,因此我可以调用一些函数:
# cffi_test.py
import cffi
INCLUDE_DIRS = ('.',)
SOURCES = ('magic.c',)
ffi = cffi.FFI()
ffi.set_source(
'_magic_tests',
'#include "magic.h"',
include_dirs = INCLUDE_DIRS,
sources = SOURCES,
libraries = [],
)
ffi.compile()
最终我计划在一组单元测试之前将其作为设置的一部分,例如。纯 Python 函数 test_add()
将通过在测试设置中构建的 ffi
object 调用并检查 C 函数 add()
的结果。
上面的脚本似乎有效;它运行没有错误,它创建了一个 _magic_tests.c
文件、一个 _magic_tests.cp35-win32.pyd
文件和一个 Release
目录。我也可以 import _magic_tests
没有错误。
但我不知道如何通过 CFFI 实际调用 C 函数。我找不到 set_source()
函数的任何文档,它似乎是整个过程不可或缺的一部分。 overview mentions it a lot, but the reference contains zero occurrences of it. The docs do have a section on calling functions,但它引用了一些 lib
object 而没有显示它是如何创建的。如果我查看前面的示例,则会有一个从 ffi.dlopen()
创建的 lib
object,但我不知道如何将其应用于 CFFI 本身正在生成的内容。
我的大问题(即我的 X problem)是:
- CFFI 是否是一个用于在跨平台(Windows 7-10,Linux,OS X)中调用和测试 C 函数的合理工具,如果它是,怎么样?
我目前的方法(即我的 Y problems)引起的问题是:
set_source()
的文档在哪里?我怎样才能知道它需要哪些参数?
- 如何生成包含我要调用的函数的
lib
object?
- 这是使用 CFFI 调用 C 函数的最简单方法吗?我不是特别需要或想要生成共享库或可再分发的包;如果它必须发生,那很好,但没有必要。我还可以尝试哪些其他方法?
我当前的设置是:
- OS: Windows 10
- Python: CPython 3.5.1 32 位
- 点:8.1.2
- CFFI: 1.6.0
- C 编译器:Visual C++ Build Tools 2015 附带的任何内容,链接自 this MSDN post
我正在使用 Christoph Gohlke's repository 的 CFFI 和 pycparser。
对于我的一个项目,我使用 cffi
来测试我的 C 代码。恕我直言 cffi
是为 C 代码生成 python 绑定的好工具,因此认为它是用于从 python 调用和测试 C 函数的合理工具。但是,您的代码只能像 C 代码一样跨平台,因为您必须为每个平台编译绑定。
您可以在下面找到一些应该回答您的问题的文档参考。此外,我还编写了一些示例代码来说明如何使用 cffi
。对于更大的示例,您可以在 https://github.com/ntruessel/qcgc/tree/master/test.
找到我的项目
可在此处找到 set_source()
的文档 https://cffi.readthedocs.io/en/latest/cdef.html
https://cffi.readthedocs.io/en/latest/overview.html解释了如何使用CFFI,我推荐API,out-of-line.
四个你的例子,build_magic_tests.py
看起来像这样:
from cffi import FFI
ffibuilder = FFI()
# For every function that you want to have a python binding,
# specify its declaration here
ffibuilder.cdef("""
int add(int a, int b);
""")
# Here go the sources, most likely only includes and additional functions if necessary
ffibuilder.set_source("magic_tests",
"""
#include "magic.h"
""", sources=["magic.c"])
if __name__ == "__main__":
ffibuilder.compile()
要生成 magic_tests 模块,您必须 运行 python build_magic_tests.py
。生成的模块可以这样导入使用:
from magic_tests import ffi, lib
def run_add():
assert 4 == lib.add(4, 5)
我有一个 C source/header 文件,它是一个更大项目的一部分。我想将其作为一个独立于实际项目的单元进行测试。虽然可以通过创建一个具有不同 main()
的新项目在 C 中做到这一点,但我想看看我是否可以使用 Python (3) 及其框架(例如 nose)来加速测试的构建,使用现有的报告框架等
我的印象是我可以用 CFFI 做到这一点。这是一个示例 C 文件:
// magic.c
// Implementation of magic.
int add(int a, int b)
{
return a;
}
header:
// magic.h
// Add two numbers (where a + b is not greater than INT_MAX).
int add(int a, int b);
这是一个试图编译它的脚本,因此我可以调用一些函数:
# cffi_test.py
import cffi
INCLUDE_DIRS = ('.',)
SOURCES = ('magic.c',)
ffi = cffi.FFI()
ffi.set_source(
'_magic_tests',
'#include "magic.h"',
include_dirs = INCLUDE_DIRS,
sources = SOURCES,
libraries = [],
)
ffi.compile()
最终我计划在一组单元测试之前将其作为设置的一部分,例如。纯 Python 函数 test_add()
将通过在测试设置中构建的 ffi
object 调用并检查 C 函数 add()
的结果。
上面的脚本似乎有效;它运行没有错误,它创建了一个 _magic_tests.c
文件、一个 _magic_tests.cp35-win32.pyd
文件和一个 Release
目录。我也可以 import _magic_tests
没有错误。
但我不知道如何通过 CFFI 实际调用 C 函数。我找不到 set_source()
函数的任何文档,它似乎是整个过程不可或缺的一部分。 overview mentions it a lot, but the reference contains zero occurrences of it. The docs do have a section on calling functions,但它引用了一些 lib
object 而没有显示它是如何创建的。如果我查看前面的示例,则会有一个从 ffi.dlopen()
创建的 lib
object,但我不知道如何将其应用于 CFFI 本身正在生成的内容。
我的大问题(即我的 X problem)是:
- CFFI 是否是一个用于在跨平台(Windows 7-10,Linux,OS X)中调用和测试 C 函数的合理工具,如果它是,怎么样?
我目前的方法(即我的 Y problems)引起的问题是:
set_source()
的文档在哪里?我怎样才能知道它需要哪些参数?- 如何生成包含我要调用的函数的
lib
object? - 这是使用 CFFI 调用 C 函数的最简单方法吗?我不是特别需要或想要生成共享库或可再分发的包;如果它必须发生,那很好,但没有必要。我还可以尝试哪些其他方法?
我当前的设置是:
- OS: Windows 10
- Python: CPython 3.5.1 32 位
- 点:8.1.2
- CFFI: 1.6.0
- C 编译器:Visual C++ Build Tools 2015 附带的任何内容,链接自 this MSDN post
我正在使用 Christoph Gohlke's repository 的 CFFI 和 pycparser。
对于我的一个项目,我使用 cffi
来测试我的 C 代码。恕我直言 cffi
是为 C 代码生成 python 绑定的好工具,因此认为它是用于从 python 调用和测试 C 函数的合理工具。但是,您的代码只能像 C 代码一样跨平台,因为您必须为每个平台编译绑定。
您可以在下面找到一些应该回答您的问题的文档参考。此外,我还编写了一些示例代码来说明如何使用 cffi
。对于更大的示例,您可以在 https://github.com/ntruessel/qcgc/tree/master/test.
可在此处找到
set_source()
的文档 https://cffi.readthedocs.io/en/latest/cdef.htmlhttps://cffi.readthedocs.io/en/latest/overview.html解释了如何使用CFFI,我推荐API,out-of-line.
四个你的例子,build_magic_tests.py
看起来像这样:
from cffi import FFI
ffibuilder = FFI()
# For every function that you want to have a python binding,
# specify its declaration here
ffibuilder.cdef("""
int add(int a, int b);
""")
# Here go the sources, most likely only includes and additional functions if necessary
ffibuilder.set_source("magic_tests",
"""
#include "magic.h"
""", sources=["magic.c"])
if __name__ == "__main__":
ffibuilder.compile()
要生成 magic_tests 模块,您必须 运行 python build_magic_tests.py
。生成的模块可以这样导入使用:
from magic_tests import ffi, lib
def run_add():
assert 4 == lib.add(4, 5)