[Cython] Fwd: Question about how best require compiler options for C sources

Erik Bray erik.m.bray at gmail.com
Fri Apr 8 07:01:33 EDT 2016


Hi all,

I'd like to call attention to an issue I've been looking into for the
past couple days:

https://github.com/cython/cython/pull/360

To summarize the discussion, when building DLLs for Windows, functions
that should be exported by that DLL must be marked
__declspec(dllexport) in their declaration.  However, when using the
same header file in a project that links to that DLL the same function
must be declared __declspec(dllimport).

It's common practice to have a macro for this, whose value is
controlled by whether or not a macro (often called something like
"DLL_EXPORT").  When compiling the DLL we would define -DDLL_EXPORT to
output __declspec(dllexport).  Otherwise it outputs
__declspec(dllimport).

Cython currently handles this with such a macro called DL_IMPORT which
comes from Python. However, this macro was deprecated some time ago,
and is removed in current Python 3 versions.  So Cython must replace
it with its own.

I'm working on a patch for this--to reduce confusion the macro is
named specifically for the Cython module it's associated with.  For
example, for a Cython module named "foo.bar" there are two macros
DLL_EXPORT__foo__bar and EXPORT__foo__bar.  If the latter is defined
then the former outputs dllexport, otherwise it outputs dllimport.
I've attached the patch in progress.

I'm open to comment on this, but where I'm stuck now is that in order
for the "foo.bar" module to be compiled correctly it needs
EXPORT__foo__bar to be defined at compile time.  And it's not clear to
me what the best way is for the Cython compiler to pass down options
that are passed to the C/C++ compiler.  In general the best way would
be to attach this to the define_macros attribute of the associated
distutils/setuptools Extension object and let the distutils compiler
class generate the right compiler options.  But it's not clear to me
from Cython's internals where the best place to do that would be.

Thanks,
Erik
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dll_export.patch
Type: application/octet-stream
Size: 4161 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20160408/087ebedb/attachment.obj>


More information about the cython-devel mailing list