How To Call D Code from Objective C?

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 11 02:00:56 PST 2015


On 2015-12-11 10:18, Mike McKee wrote:
> Well, part of it was that I needed to do -shared on Linux instead of
> -dynamiclib. On OSX, I have to use -dynamiclib.
>
> It compiles now on the C code, but when I created a cmain.c to test the
> library, it gave me a segmentation fault when trying to run this
> compiled program, even though I copied gfunc.o and cfunc.o to /usr/lib
> on Linux.
>
> // cmain.c
> #include <stdio.h>
> extern char * c_dfunc(char *s);
>
> void main() {
>          printf("%s\n",c_dfunc("request"));
> }

There are several issues:

1. The D function needs to use C compatible types, see my answer to your 
previous post

2. You need to initialize the D runtime from the C side

3. If you allocate data in D using the GC and pass it to the C side, you 
need to make sure there's a reference to it on the D side. Otherwise the 
GC could collect the memory

4. Dynamic libraries are not yet supported on OS X

I recommend asking these question in the learning forum. As a first step 
you need to learn how to interact between D and C. You can get a lot of 
answers for that on the learning forum, tutorials, documentation, books 
and so on.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list