Conditional compilation

Anthony Goins neontotem at gmail.com
Fri Jun 7 20:01:29 PDT 2013


On Friday, 7 June 2013 at 12:20:23 UTC, finalpatch wrote:
> string mixins and template mixins don't work either.
>
> On Friday, 7 June 2013 at 12:14:45 UTC, finalpatch wrote:
>> 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?

string mixin will work

import std.stdio;
enum :string
{
      //define relevant functions in this string
      x1 =
      `void testfunc(string str)
      {
           writeln(str);
      }`
}


version(X)
{
      extern(C) mixin(x1);
      string str2 = "c version";
}

version(Y)
{
      extern(Windows) mixin(x1);
      string str2 = "windows version";
}

void main()
{
      testfunc(str2);
}

Yes it's ... well ... not very ... umm
But it works :)
extern(system) MIGHT be a better option.


More information about the Digitalmars-d-learn mailing list