I Did It! Calling D Library from Objective C in XCode on OSX

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 14 23:52:50 PST 2015


On 2015-12-14 20:20, Mike McKee wrote:

> Oh, I found I could do:
>
> $ sudo brew update
> $ sudo brew upgrade dmd

Alternatively you can install DMD using DVM [1].

> Now it generates this error:
>
> $ dmd -m64 -L-framework -LFoundation test.d
> test.d(6): Error: undefined identifier 'selector'
> test.d(12): Error: undefined identifier 'selector'
> test.d(13): Error: undefined identifier 'selector'
>
> Here's the source I'm trying to compile, and evidently it doesn't like
> @selector for some reason:
>
> // test.d
> module main;
>
> extern (Objective-C)
> interface Class
> {
>      NSString alloc() @selector("alloc");
> }
>
> extern (Objective-C)
> interface NSString
> {
>      NSString initWithUTF8String(in char* str)
> @selector("initWithUTF8String:");
>      void release() @selector("release");
> }
>
> extern (C) void NSLog(NSString, ...);
> extern (C) Class objc_lookUpClass(in char* name);
>
> void main()
> {
>      auto cls = objc_lookUpClass("NSString");
>      auto str = cls.alloc().initWithUTF8String("Hello World!");
>      NSLog(str);
>      str.release();
> }

Hmm, that should work. @selector is defined in core.attributes which 
should be imported automatically through the object module. Perhaps the 
compiler doesn't pick up the correct version of druntime.

Could you please add "-v" do the command line when compiling. Look in 
the beginning of the output for "import object". If you open the 
"object" module, it should contain a line like this, in the beginning of 
the file (line 52 for me) :

version (D_ObjectiveC) public import core.attribute : selector;

Then in the "attribute" module (which should come right after the 
"object" module in the output) should contain this, in the bottom of the 
file:

version (D_ObjectiveC) struct selector
{
     string selector;
}

My output with the "-v" flag looks like this:

$ dmd -v main.d
binary    dmd
version   v2.069.1
config    /Users/jacob/.dvm/compilers/dmd-2.069.1/osx/bin/dmd.conf
parse     main
importall main
import    object 
(/Users/jacob/.dvm/compilers/dmd-2.069.1/osx/bin/../../src/druntime/import/object.d)
import    core.attribute 
(/Users/jacob/.dvm/compilers/dmd-2.069.1/osx/bin/../../src/druntime/import/core/attribute.d)
import    std.stdio 
(/Users/jacob/.dvm/compilers/dmd-2.069.1/osx/bin/../../src/phobos/std/stdio.d)

You can also see the config file used in the beginning of the output 
above. That tells the compiler where do find the druntime.

Please try all these commands directly on the command line without using 
Xcode in anyway.

[1] https://github.com/jacob-carlborg/dvm

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list