如何使用cffi导入本地库

时间:2015-01-27 09:45:20

标签: python python-3.x python-cffi

我有一个test.c,如下所示(在与python文件相同的目录中)

#include <stdio.h>
void myprint();  
void myprint()
{
printf("hi\n");
}

现在我想把它“导入”我的python脚本

from cffi import FFI
ffi = FFI()
ffi.cdef("void myprint();")
C = ffi.verify("""
#include "test.c"
""")

使用cffi文档中的示例可以正常工作。

很抱歉大回溯错误......

__pycache__/_cffi__xd52cd3eax2b8a27ce.c:185:18: fatal error: test.c: No such file or directory
 #include "test.c"
                  ^
compilation terminated.
Traceback (most recent call last):
  File "/usr/lib/python3.4/distutils/unixccompiler.py", line 116, in _compile
    extra_postargs)
  File "/usr/lib/python3.4/distutils/ccompiler.py", line 909, in spawn
    spawn(cmd, dry_run=self.dry_run)
  File "/usr/lib/python3.4/distutils/spawn.py", line 36, in spawn
    _spawn_posix(cmd, search_path, dry_run=dry_run)
  File "/usr/lib/python3.4/distutils/spawn.py", line 162, in _spawn_posix
    % (cmd, exit_status))
distutils.errors.DistutilsExecError: command 'x86_64-linux-gnu-gcc' failed with exit status 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/cffi/ffiplatform.py", line 48, in _build
    dist.run_command('build_ext')
  File "/usr/lib/python3.4/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/lib/python3.4/distutils/command/build_ext.py", line 348, in run
    self.build_extensions()
  File "/usr/lib/python3.4/distutils/command/build_ext.py", line 457, in build_extensions
    self.build_extension(ext)
  File "/usr/lib/python3.4/distutils/command/build_ext.py", line 512, in build_extension
    depends=ext.depends)
  File "/usr/lib/python3.4/distutils/ccompiler.py", line 574, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "/usr/lib/python3.4/distutils/unixccompiler.py", line 118, in _compile
    raise CompileError(msg)
distutils.errors.CompileError: command 'x86_64-linux-gnu-gcc' failed with exit status 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pynfc_nfx.py", line 11, in <module>
    """)
  File "/usr/local/lib/python3.4/dist-packages/cffi/api.py", line 340, in verify
    lib = self.verifier.load_library()
  File "/usr/local/lib/python3.4/dist-packages/cffi/verifier.py", line 74, in load_library
    self._compile_module()
  File "/usr/local/lib/python3.4/dist-packages/cffi/verifier.py", line 139, in _compile_module
    outputfilename = ffiplatform.compile(tmpdir, self.get_extension())
  File "/usr/local/lib/python3.4/dist-packages/cffi/ffiplatform.py", line 25, in compile
    outputfilename = _build(tmpdir, ext)
  File "/usr/local/lib/python3.4/dist-packages/cffi/ffiplatform.py", line 51, in _build
    raise VerificationError('%s: %s' % (e.__class__.__name__, e))
cffi.ffiplatform.VerificationError: CompileError: command 'x86_64-linux-gnu-gcc' failed with exit status 1

2 个答案:

答案 0 :(得分:2)

fatal error: test.c: No such file or directory

试试ffi.verify(..., include_dirs=['.'])。编译器不是从当前目录调用的,而是从cffi创建临时文件的某个临时目录中调用的。

答案 1 :(得分:1)

你在printf("hi")之后忘记了分号。 (你也忘了#include <stdio.h>\n之后可能忘了hi,但这些不应该给你编译错误。)