runtime evaluation

hoya sighoya at gmail.com
Sat Dec 14 12:11:46 PST 2013


On Saturday, 14 December 2013 at 17:47:34 UTC, Marco Leise wrote:
> Am Sat, 14 Dec 2013 14:59:42 +0100
> schrieb "hoya" <sighoya at gmail.com>:
>
>> On Saturday, 14 December 2013 at 13:44:01 UTC, Marco Leise 
>> wrote:
>> 
>> > You can load libraries at runtime like OpenGL or a database
>> > driver. This applies to all C and D libraries as well as C++
>> > libraries to some extent.
>> >
>> Can you give me an example how to do this?
>
> Hmm, there are two options when it comes to dynamic linking.
>
> You can directly refer to the functions you use from some
> library. An example is std.net.curl in Phobos. You compile
> your program with "dmd -L-lcurl <source>" to link to the curl
> library which is written in C and common on Linux systems.
> This is the simple case where you don't need to write any
> extra code.
>
> If you want to load a plugin at runtime or OpenGL (which is a
> different library for each graphics card vendor) you will need
> to use the dynamic linker (libdl.so) which handles this case:
> "dmd -L-ldl <source>" and then use "dlopen" and "dlsym" in
> your program to find the addresses of functions. Derelict 3 is
> a good example of loading libraries at runtime this way:
> https://github.com/aldacron/Derelict3
> It is a wrapper/binding collection for many multimedia/game
> related libraries and you can reuse its derelict.util module
> to load your own libraries at runtime.
>
> Which case are you interested in?
>
The Latter
> This can be done with dmd as
> well, but in both cases it is crutch to bring eval()-like
> functionality to a statically compiled language. :)
Hmm. An eval() method is good, if writing something like a Drepl. 
I've heard from someone that other people have tried some work on 
it.
Or good case, if you can use a dynamic mixin(). When read a 
string from input, then it is possible to create a structure like 
struct, class and so on. With dlopen() you need at least a 
library file, where all your code is written in, as I thin 
correctly. But with a dynamic mixin() you have the ability, to 
add structures in-memory. So this is more efficient.



More information about the Digitalmars-d mailing list