C binding with D function
bachmeier via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 3 08:47:48 PDT 2016
On Wednesday, 3 August 2016 at 15:24:56 UTC, llaine wrote:
> On Wednesday, 3 August 2016 at 15:14:24 UTC, Kagamin wrote:
>> On Wednesday, 3 August 2016 at 15:08:51 UTC, llaine wrote:
>>> So basically I have to create wrapper.c ?
>>
>> Yes, but you should write it in D. Runtime initialization is
>> at https://dlang.org/phobos/core_runtime.html#.Runtime
>
> Okay I tried to do something like this
>
>
> import std.stdio;
> import core.runtime;
>
> extern(C)
> {
>
> void foo(string str)
> {
> writeln(str);
> }
>
> }
>
> void main(){
> Runtime.initialize();
> Runtime.terminate();
> }
>
> Am I doing it wrong ?
> Still getting a Segmentation fault
I have no way to test this, but translating the example to D, I
get this:
import core.runtime;
import std.stdio;
extern(C) {
example_init() {
Runtime.initialize();
}
example_exit() {
Runtime.terminate();
}
void foo(int x) {
writeln(2*x);
}
}
// No main
// compile as libffi-example.so
Then in Ruby:
module Example
extend DL::Importable
dlload "./libffi-example.so"
extern "void example_init()"
extern "void example_exit()"
extern "void foo(int)"
end
Example.example_init
Example.foo(3)
Example.example_exit
More information about the Digitalmars-d-learn
mailing list