Note: This website is archived. For up-to-date information about D projects and development, please visit

Ticket #30 (assigned enhancement)

Opened 12 years ago

Last modified 11 years ago

Load from directory

Reported by: darkuranium Assigned to: aldacron (accepted)
Priority: minor Version: 2.0
Keywords: directory loading Cc:


--- Copied from ---

Hey, I would like to suggest a new method for GenericLoader?:

 * Same is load, but looks in ./dir/*
void loadDir(char[] dir, char[] libNameString = null)

I tried to make a wrapper for it, but unfortunately winLibs/linLibs/macLibs are not publically available (and I did not want to modify DerelictUtil?.d because that would break compatibility).

Why would this be useful? Well, I got a TON of DLLs right now, and it would be really nice to be able to put them into a separate directory without doing stuff like:

else version(linux)
else version(darwin)
    DerelictIL.load(...); // okay, I don't know how Mac libs are named

...for each DLL


patch.patch (1.5 kB) - added by Silverclaw on 02/09/10 19:43:40.
patch to add functionality
addDir.patch (1.3 kB) - added by Silverclaw on 02/10/10 19:26:59.
draft only: undocumented, untested

Change History

01/07/09 09:32:24 changed by aldacron

  • owner set to aldacron.
  • status changed from new to assigned.

I'll definitely take this under consideration. Perhaps a global property.

02/01/10 13:29:54 changed by aldacron

  • version set to 2.0.

02/07/10 13:07:59 changed by Silverclaw

I may be able to add a function "addSearchDir" to add new lib search paths to the load() function. Or at least change the load(string) to check if the name given is a directory, and if it is, to search inside it.

I'd like to know if this enhancement is still under consideration and if the loader system is going to be changed (rendering my changes useless).

02/07/10 16:00:10 changed by aldacron

Yes, I'm definitely wanting to implement this. I have an idea of how I want to do it, but I need to read up on the behavior of ldopen.

What I'm thinking is to maintain a global search path, which can be appended to via a function like your addSearchDir. This would work as a static method on the loader, or a free function. Doesn't really matter. Then the loader grabs a name from the libname list, and prepends each dir on the path in turn until it loads. There also needs to be an option to try the default OS search path if not found on the custom path.

02/07/10 16:06:20 changed by Silverclaw

The OS will typically automatically look on default folders if none is specified.

Give me a while for me to come up with a addSearchDir similar function and I'll post a patch here for you to check out.

02/09/10 19:43:12 changed by Silverclaw

Here is my draft for this functionality.

It's solely dependant on Phobos and done for D2. It should be modified to work with Tango (which I have no experience on).

I am unsure how libraries are loaded on Mac, so further consideration on this regard is necessary.

02/09/10 19:43:40 changed by Silverclaw

  • attachment patch.patch added.

patch to add functionality

02/10/10 02:58:31 changed by aldacron

Looking over the code, this doesn't appear to be doing what it's supposed to. First, all we want to do is set the search directory. It shouldn't matter whether or not any of the Derelict libraries exist in the directory or not. People are free to use the loader outside of Derelict to load other libraries.

Second, making sure the directory is valid *is* a good idea for the function at runtime. So it shouldn't be a compile-time assert, nor should we rely on the caller to verify it.

Third, as written, you're overwriting libNames on every iteration of the foreach, rather than appending.

Fourth, this should be a global free function or a static method of the loader. We want a global search path, not one per library.

Ultimately, this function is doing too much. All it should do is something like this:


searchPath ~= "," ~ dir;

Nothing more. Then the loader needs to be modified such that when it is called with a given library name, it will check if the search path has been set and, if so, attempt to load the given library from each directory on the path.

02/10/10 19:23:25 changed by Silverclaw

First, i mis-understood the objective. I assumed that the starter of the ticket wanted to load from a specific location and nowhere else, that is what the function does: it "sets" the load directory. What the OP wanted was to "add" a new directory, my mistake.

Secondly, checking if the dir is valid introduces overhead, specially because a serious programmer will either check if the dir exists before calling the function or otherwise make sure it exists. Also, this assumption is documented on the documentation. Its our responsability to make the functions do what they claim to do, not fix the programmer's carelessness (that is why debug asserts exist).

Third, _libNames is not overwritten evey iteration. It is overwritten only once, and when it is, the function exits, having completed its task. As I said on the first paragraph, I misunderstood the task of the function. For clarification on what I assumed, check .

Your "fourth" point served to clarify your intention. I see what you meant and again, it misinterpreted.

Will post a new suggestion in a sec.

02/10/10 19:26:59 changed by Silverclaw

  • attachment addDir.patch added.

draft only: undocumented, untested