How to use std. packages in so files written in dlang

grampus via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 18:59:05 PDT 2016


On Friday, 12 August 2016 at 01:45:29 UTC, Mike Parker wrote:
> On Friday, 12 August 2016 at 01:36:34 UTC, grampus wrote:
>> On Friday, 12 August 2016 at 01:09:47 UTC, ketmar wrote:
>>> On Friday, 12 August 2016 at 00:57:42 UTC, grampus wrote:
>>>
>>> it's 'cause you didn't initialized druntime. you have to use 
>>> dlsym to get "rt_init" function and call it right after 
>>> loading your .so, but before calling any other API from it.
>>>
>>> also, note that druntime is using SIGUSR1 and SIGUSR2 for 
>>> it's internal housekeeping, so don't use this signals in your 
>>> C++ code.
>>
>> Thank you for the quick reply.
>> Do you mean that I have to do some changes on the C side?
>> I can use dlang in this existing project as long as nothing 
>> can be changed on the C side.
>> Do you think I can use dlang in our case?
>>
>> Thank you
>
> One way to handle this is to add initialize/terminate functions 
> to your shared library. In that case, rather than rt_init, you 
> can call the Runtime functions [1]:
>
> import core.runtime;
>
> extern(C) int initialize() { return Runtime.initialize(); }
> extern(C) int terminate() { return Runtime.terminate(); }
>
> From your C program, call initialize after loading the library 
> and terminate before exiting.
>
> [1] http://dlang.org/phobos/core_runtime.html#.Runtime

I will try this solution.
One more question, if Runtime.initialize(); is called but when 
sofile is unloaded without calling Runtime.terminate(); is it 
going to cause any problem? for example so file is loaded again 
and again, Runtime.initialize() is called many times without 
Runtime.terminate().


More information about the Digitalmars-d-learn mailing list