Conditional compilation

finalpatch fengli at gmail.com
Fri Jun 7 05:14:40 PDT 2013


Hi folks,

I need to apply different calling conventions to the same 
interfaces when compiling for different platform. It's something 
like this:

OSX:

interface InterfaceA : IUnknown
{
extern(C):
     ...
}

Windows:

interface InterfaceA : IUnknown
{
     ...
}

I have to add extern(C) on OSX because apparently when the 
compiler sees IUnknown it automatically assumes the calling 
convention is extern(Windows) and in order to maintain 
compatibility with existing system I have to explicitly declare 
them as extern(C).

Now I have several dozens of interfaces like the above. I don't 
want to repeat them for OSX and Windows because the interface 
definitions are identical except the extern(C) line.

I have tried using version() like this:

interface InterfaceA : IUnknown {
     version(OSX)
     {
         extern(C):
     }
     ...methohds.
}

but it doesn't work because version limits the scope of the 
extern(C). static if has the same problem.

In C/C++ this is really easy, I can simply define a macro which 
expands to extern(C) on OSX and nothing on windows. Is there any 
way to achieve this in D without repeating the interface 
definitions?


More information about the Digitalmars-d-learn mailing list