On 2015-12-11 09:51, Mike McKee wrote:
So I'm having trouble figuring out the D and C code.

Created dfunc.d with this:

extern (C) string dfunc(string s) {
         return s ~ "response";
}

Then compiled:

$ dmd -c dfunc.d

This created dfunc.o without error.

Next, I created C code like so:

extern char * dfunc(char *);
char * c_dfunc(char *s) {
   return dfunc(s);
}

"string" in D is not the same as "char*" in C. "string" is an alias to an array of immutable characters. An array in D consists of the length of the array and a pointer to the data.

The interface of the function needs to only contain C types. Something like this:

extern (C) char* dfunc(char* s);

If you want to use the D string operations, like ~, you need to convert it to a D string, do the concatenation, and then convert it back to a C string [2] [3].

See [3] for more information.

[1] http://dlang.org/phobos/std_string.html#.toStringz
[2] http://dlang.org/phobos/std_string.html#.fromStringz
[3] http://dlang.org/spec/interfaceToC.html

--
/Jacob Carlborg

Reply via email to