Creating and loading D plugins in D app

Patrick Schluter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 3 04:58:05 PDT 2017


On Saturday, 3 June 2017 at 09:41:58 UTC, aberba wrote:
> On Friday, 2 June 2017 at 16:36:34 UTC, H. S. Teoh wrote:
>> On Fri, Jun 02, 2017 at 12:19:48PM +0000, Adam D. Ruppe via 
>> Digitalmars-d-learn wrote:
>>> On Friday, 2 June 2017 at 11:09:05 UTC, aberba wrote:
>>> > 1. Get shared libs to work in D (the best approach for all 
>>> > D code)
>>> 
>>> I have done very little with this myself but other people 
>>> have so it is doable.
>> [...]
>>
>> This is not directly related to the OP's question, but 
>> recently I wrote a program that, given a user-specified 
>> string, transforms it into D code using a code template, 
>> invokes dmd to compile it into a shared object, loads the 
>> shared object using dlopen(), and looks up the generated 
>> function with dlsym() to obtain a function pointer that can be 
>> used for calling the function. The shared object is unloaded 
>> after it's done.
>
> Will be of much use to me to see the brief instructions for 
> this. I saw the C style on Wikipedia.
>
> Seems the functions loaded needs to be casted from void* to a 
> type... before calling. Didn't quite understand that part.
>
Yes, dlsym() returns the address of the object of the shared 
library you requested. The first parameter is the handle to the 
shared object that had been loaded by dlopen(). The second 
parameter is the name of the object one wants the address of.
The address must then be casted to the type of the object. If the 
name was one of a function, one has to cast to a function 
pointer. That's something illegal in strict C (i.e. undefined 
behaviour.) but is something that is required by Posix. It's 
really not very difficult.


More information about the Digitalmars-d-learn mailing list