cffi.error.FFIError:函数的多个声明

时间:2018-11-03 14:28:34

标签: python python-cffi

我正在使用CFFI使用API​​-离线方法自动包装DLL。

这实际上是通过持续集成完成的,到目前为止效果很好。为了实现这一点,将在Visual Studio中对DLL头文件进行预处理,并在对FFI.cdef的调用中使用其输出。

我们的DLL最近由于包含complex.h而变得更加复杂,这导致了一些困难。

我现在遇到的一个问题是CFFI似乎不喜欢重复的函数定义。

我收到此错误:cffi.error.FFIError: multiple declarations of function cimag

当我查看预处理的头文件时,确实可以看到此声明的重复项:

double __cdecl cimag( _Dcomplex _Z);

请注意,这只是对具有相同签名的相同功能的重新声明。重复项似乎是作为预处理步骤的一部分添加的。 尽管这是完全合法的C语言,并且编译没有问题。

如何使CFFI对此感到满意?

我已经在对预处理后的头文件进行后处理,以处理Microsoft特定的添加内容,例如:

  1. declspec(noreturn)(只是删除这些出现的内容)
  2. __w64(只是删除这些出现的内容)
  3. __int64(为int64_t添加typedef

更新

快速浏览了CFFI的源代码(引发异常的位置)之后,我发现了一个关键字:override,可以将其提供给ffi.cdef。添加override=True可以使包装过程继续进行,但是现在我在自动生成的包装C文件中遇到了语法错误:

(pywrapper) c:\projects\wrapper_proj\pywrapper>python -m release_process.cffi_wrap_dll -h
generating .\pywrapper\dll_wrapper\_command.c
(already up-to-date)
the current directory is 'c:\\projects\\wrapper_proj\\pywrapper'
running build_ext
building 'pywrapper.dll_wrapper._command' extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Ic:\projects\wrapper_proj\pywrapper\pywrapper\dll_wrapper -IC:\Users\user\python_envs\pywrapper\include -IC:\Python37-32\include -IC:\Python37-32\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" "-IC:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /Tcpywrapper\dll_wrapper\_command.c /Fo.\Release\pywrapper\dll_wrapper\_command.obj
_command.c
pywrapper\dll_wrapper\_command.c(11262): error C2143: syntax error: missing ')' before '*'
pywrapper\dll_wrapper\_command.c(11262): error C2143: syntax error: missing '{' before '*'
pywrapper\dll_wrapper\_command.c(11262): error C2059: syntax error: ')'
pywrapper\dll_wrapper\_command.c(11263): error C2054: expected '(' to follow 'p'
pywrapper\dll_wrapper\_command.c(11269): error C2061: syntax error: identifier '_locale_tstruct'
pywrapper\dll_wrapper\_command.c(11269): error C2059: syntax error: '}'
pywrapper\dll_wrapper\_command.c(11272): error C2143: syntax error: missing ')' before '*'
pywrapper\dll_wrapper\_command.c(11272): error C2143: syntax error: missing '{' before '*'
pywrapper\dll_wrapper\_command.c(11272): error C2059: syntax error: ')'
pywrapper\dll_wrapper\_command.c(11273): error C2054: expected '(' to follow 'p'
pywrapper\dll_wrapper\_command.c(11281): error C2061: syntax error: identifier 'locrefcount'
pywrapper\dll_wrapper\_command.c(11281): error C2059: syntax error: '}'
pywrapper\dll_wrapper\_command.c(11284): error C2143: syntax error: missing ')' before '*'
pywrapper\dll_wrapper\_command.c(11284): error C2143: syntax error: missing '{' before '*'
pywrapper\dll_wrapper\_command.c(11284): error C2059: syntax error: ')'
pywrapper\dll_wrapper\_command.c(11285): error C2054: expected '(' to follow 'p'
pywrapper\dll_wrapper\_command.c(11307): error C2061: syntax error: identifier 'threadlocinfo'
pywrapper\dll_wrapper\_command.c(11307): error C2059: syntax error: '}'
pywrapper\dll_wrapper\_command.c(11564): error C2065: '_locale_tstruct': undeclared identifier
pywrapper\dll_wrapper\_command.c(11564): error C2059: syntax error: ')'
pywrapper\dll_wrapper\_command.c(11569): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11572): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11575): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11578): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11581): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11584): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11587): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11590): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11593): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11596): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11599): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11602): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11605): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11608): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11611): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11614): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11617): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11620): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11623): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11626): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11629): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11632): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11635): error C2059: syntax error: ','
pywrapper\dll_wrapper\_command.c(11650): error C2065: '_locale_tstruct': undeclared identifier
pywrapper\dll_wrapper\_command.c(11650): error C2037: left of 'y' specifies undefined struct/union '_cffi_align___locale_tstruct'
pywrapper\dll_wrapper\_command.c(11652): error C2065: 'locrefcount': undeclared identifier
pywrapper\dll_wrapper\_command.c(11652): error C2037: left of 'y' specifies undefined struct/union '_cffi_align__locrefcount'
pywrapper\dll_wrapper\_command.c(11654): error C2065: 'threadlocinfo': undeclared identifier
pywrapper\dll_wrapper\_command.c(11654): error C2037: left of 'y' specifies undefined struct/union '_cffi_align__threadlocinfo'
pywrapper\dll_wrapper\_command.c(11649): fatal error C1903: unable to recover from previous error(s); stopping compilation
Internal Compiler Error in C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe.  You will be prompted to send an error report to Microsoft later.
INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe'
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information
Traceback (most recent call last):
  File "C:\Python37-32\lib\distutils\_msvccompiler.py", line 423, in compile
    self.spawn(args)
  File "C:\Python37-32\lib\distutils\_msvccompiler.py", line 542, in spawn
    return super().spawn(cmd)
  File "C:\Python37-32\lib\distutils\ccompiler.py", line 909, in spawn
    spawn(cmd, dry_run=self.dry_run)
  File "C:\Python37-32\lib\distutils\spawn.py", line 38, in spawn
    _spawn_nt(cmd, search_path, dry_run=dry_run)
  File "C:\Python37-32\lib\distutils\spawn.py", line 81, in _spawn_nt
    "command %r failed with exit status %d" % (cmd, rc))
distutils.errors.DistutilsExecError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\cl.exe' failed with exit status 2

During handling of the above exception, another exception occurred:

我可以在预处理的头文件中看到locale_t的定义:

typedef struct __crt_locale_data_public
{
      unsigned short const* _locale_pctype;
      int _locale_mb_cur_max;
               unsigned int _locale_lc_codepage;
} __crt_locale_data_public;

typedef struct __crt_locale_pointers
{
    struct __crt_locale_data*    locinfo;
    struct __crt_multibyte_data* mbcinfo;
} __crt_locale_pointers;

typedef __crt_locale_pointers* _locale_t; 

1 个答案:

答案 0 :(得分:0)

最后,我放弃了尝试对包含<comlex.h>的头文件进行预处理的细微差别。

相反,我要为其添加#include,然后自己将其替换为适当的原语,以在公开函数中容纳对复杂类型的引用。

它是一种hack,但可以使用。