本文介绍了给定源代码,如何使用 CFFI 调用现有的 C 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个 C 源/头文件,它是一个更大项目的一部分.我想将其作为一个独立于实际项目的单元进行测试.虽然可以通过使用不同的 main() 创建一个新项目在 C 中执行此操作,但我想看看是否可以使用 Python (3) 及其框架(例如鼻子)加快测试的构建,使用现有的报告框架等.

I have a C source/header file that are part of a bigger project. I would like to test this as a unit, independent of the real project. While it would be possible to do this in C by creating a new project with a different main(), I would like to see if I can use Python (3) and its frameworks (eg. nose) to accelerate the construction of tests, use existing reporting frameworks, etc.

我的印象是我可以用 CFFI 做到这一点.这是一个示例 C 文件:

I was under the impression that I could do this with CFFI. Here's a sample C file:

// magic.c
// Implementation of magic.
int add(int a, int b)
{
    return a;
}

标题:

// magic.h
// Add two numbers (where a + b is not greater than INT_MAX).
int add(int a, int b);

这是一个尝试编译它的脚本,以便我可以调用一些函数:

Here's a script that just tries to compile it so I can call some functions:

# 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 对象调用和检查 C 函数 add() 的结果,该对象在测试设置.

Ultimately I plan to have this be part of the setup before a set of unit tests eg. a pure Python function test_add() will call and check the result of the C function add() via the ffi object, which is constructed in the test setup.

上面的脚本似乎可以工作;它运行没有错误,它创建了一个 _magic_tests.c 文件、一个 _magic_tests.cp35-win32.pyd 文件和一个 Release 目录.我也可以import _magic_tests 没有错误.

The above script seems to work; it runs without error, it creates a _magic_tests.c file, a _magic_tests.cp35-win32.pyd file, and a Release directory. I can also import _magic_tests without an error.

但我不知道如何通过 CFFI 实际调用 C 函数.我找不到 set_source() 函数的任何文档,它似乎是整个过程不可或缺的一部分.概述 提到了很多,但 reference 包含它的零次出现.文档do有一个关于调用的部分函数,但它引用了一些 lib 对象,而没有显示它是如何创建的.如果我看前面的例子,有一个从 ffi.dlopen() 创建的 lib 对象,但我不知道如何将它应用于 CFFI 本身正在生成的东西.

But I can't figure out how to actually call a C function via CFFI. I can't find any documentation for the set_source() function, and it seems pretty integral to the whole process. The overview mentions it a lot, but the reference contains zero occurrences of it. The docs do have a section on calling functions, but it refers to some lib object without showing how it's created. If I look at the previous example there's a lib object created from ffi.dlopen(), but I don't see how to apply that to something that CFFI itself is producing.

我的大问题(即我的X问题)是:

My big question (ie. my X problem) is:

  • CFFI 是否是用于在跨平台(Windows 7-10、Linux、OS X)中调用和测试 C 函数的合理工具,如果是,如何使用?

我目前的方法(即我的Y问题)产生的问题是:

The questions arising from my current approach (ie. my Y problems) are:

  • set_source() 的文档在哪里?我怎样才能找出它需要什么参数?
  • 如何生成包含要调用的函数的 lib 对象?
  • 这是使用 CFFI 调用 C 函数的最简单方法吗?我并不特别需要或不希望生成共享库或可再发行包;如果必须发生,那很好,但没有必要.我还可以尝试哪些其他方法?
  • Where is the documentation for set_source()? How can I find out what arguments it takes?
  • How do I produce lib objects that contain the functions I want to call?
  • Is this the easiest way to use CFFI to call a C function? I do not particularly need or want a shared library or redistributable package to be produced; if it has to happen, that's fine, but it's not necessary. What other approaches could I try?

我目前的设置是:

  • 操作系统:Windows 10
  • Python:CPython 3.5.1 32 位
  • 点子:8.1.2
  • CFFI:1.6.0
  • C 编译器:Visual C++ Build Tools 2015 随附的任何内容,链接自 这篇 MSDN 帖子

我正在使用 Christoph Gohlke 的存储库中的 CFFI 和 pycparser.

I am using CFFI and pycparser from Christoph Gohlke's repository.

推荐答案

对于我的一个项目,我使用 cffi 来测试我的 C 代码.恕我直言 cffi 是一个很好的工具,可以为 C 代码生成 python 绑定,因此认为它是用于从 python 调用和测试 C 函数的合理工具.但是,您的代码只能像 C 代码一样跨平台,因为您必须为每个平台编译绑定.

For a project of mine, I use cffi to test my C code. IMHO cffi is a great tool to generate python bindings for C code and therefore think that it is a reasonable tool to use for calling and testing C functions from python. However, your code will only be as cross platform as the C code is, since you have to compile the binding for every platform.

您可以在下面找到一些可以回答您的问题的文档参考.此外,我编写了一些示例代码来说明您将如何使用 cffi.对于更大的示例,您可以在 https://github.com/ntruessel/中找到我的项目qcgc/tree/master/test.

Below you can find a few references to the documentation that should answer your questions. Additionally I wrote some example code to illustrate how you would use cffi. For a larger example, you can find my project at https://github.com/ntruessel/qcgc/tree/master/test.

  • The documentation for set_source() can be found here https://cffi.readthedocs.io/en/latest/cdef.html

https://cffi.readthedocs.io/en/latest/overview.html 解释了如何使用 CFFI,我推荐 API,外线.

https://cffi.readthedocs.io/en/latest/overview.html explains how you can use CFFI, I recommend API, out-of-line.

您的四个示例,build_magic_tests.py 看起来像这样:

Four your example, build_magic_tests.py would look something like this:

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.生成的模块可以像这样导入和使用:

To generate the magic_tests module, you have to run python build_magic_tests.py. The generated module can be imported and used like this:

from magic_tests import ffi, lib

def run_add():
    assert 4 == lib.add(4, 5)

这篇关于给定源代码,如何使用 CFFI 调用现有的 C 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:21