Windows embedding - error C2375 redefinition, different linkage

140 views
Skip to first unread message

Александр Семенов

unread,
Feb 22, 2016, 3:43:18 AM2/22/16
to python-cffi
Hi, 

Great work! Embedding cffif opens new worlds for python. But I've got errors on example.
My environment is Windows 10 x64, VS2015, Python 3.5 (x64)

When I try example from Overview (http://cffi.readthedocs.org/en/latest/overview.html#embedding) all seems goes ok.

When I try example from embedding (http://cffi.readthedocs.org/en/latest/embedding.html#usage) I've copied/pasted plugin.h+plugin_build.py exactly from doc, but compilation fails, I've got my_plugin.c but neither any of .dll nor any files in ./Release subdirectory. Errors looks like this:

C:\Users\Sav\Wrk\cf>plugin_build.py
running build_ext
building 'my_plugin' extension
creating Release
C:\VS2015\VC\BIN\amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Python35\include -IC:\Python35\include -IC:\VS2015\VC\INCLUDE -IC:\VS2015\VC\ATLMFC\INCLUDE "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10586.0\winrt" /Tcmy_plugin.c /Fo.\Release\my_plugin.obj
my_plugin.c
my_plugin.c(983): error C2375: 'do_stuff': redefinition; different linkage
c:\users\sav\wrk\cf\plugin.h(3): note: see declaration of 'do_stuff'
Traceback (most recent call last):
  File "C:\Python35\lib\distutils\_msvccompiler.py", line 395, in compile
    self.spawn(args)
  File "C:\Python35\lib\distutils\_msvccompiler.py", line 514, in spawn
    return super().spawn(cmd)
  File "C:\Python35\lib\distutils\ccompiler.py", line 909, in spawn
    spawn(cmd, dry_run=self.dry_run)
  File "C:\Python35\lib\distutils\spawn.py", line 38, in spawn
    _spawn_nt(cmd, search_path, dry_run=dry_run)
  File "C:\Python35\lib\distutils\spawn.py", line 81, in _spawn_nt
    "command %r failed with exit status %d" % (cmd, rc))
distutils.errors.DistutilsExecError: command 'C:\\VS2015\\VC\\BIN\\amd64\\cl.exe' failed with exit status 2

Please help me find problem.

Александр Семенов

unread,
Feb 22, 2016, 4:30:30 AM2/22/16
to python-cffi
-----------------------------
# file plugin_build.py
import re
import cffi
ffi = cffi.FFI()

with open('plugin.h') as f:
    include = f.read()

ffi.embedding_api(include)

ffi.set_source("my_plugin", 
        re.sub(r'^extern(?=\s)', 'CFFI_DLLEXPORT', include, flags=re.M))
-----------------------------
seems fixes situation, but documentation says "This is automatic if this code contains a line like #include "plugin.h" in the example above."

Matti Picus

unread,
Feb 22, 2016, 11:45:23 AM2/22/16
to python-cffi
It seems you hit a documentation issue.
The "This" in "This is automatic ...." refers to type existence, not to the proper linkage specifier. The declaration for windows must match the definition, and 'external' is not equivalent to CFFI_DLLEXPORT here.

Armin Rigo

unread,
Feb 23, 2016, 3:11:25 AM2/23/16
to pytho...@googlegroups.com
Hi Matti,
It's a Windows-only issue that you can't say first "extern int foo(int);"
and then "CFFI_DLLEXPORT int foo(int);". I naively assumed it would
work everywhere based on checking on other platforms. So we need to
see if there is any possible workaround, or if this must be documented
explicitly.

It seems that Windows headers for functions declared by DLLs are
written in one of two styles:

* "__declspec(dllexport) int foo(int);" in the first place, without
any "extern int foo(int);"

* or by putting the name "foo" in an extra .def file --- then the
header really contains "extern int foo(int);", I guess? Unsure. If
someone could provide a small but complete example of this, it would help!


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages