Haydn V. Harach
Haydn V. Harach

Reputation: 1275

How can I change the .dll search path my application uses?

As my application grows, I find myself using more and more third-party libraries, and the number of .dll's in my application directory is growing (11 at the time of this writing). This isn't really a problem, per se, but it is ugly. I would much rather be able to put these in "bin/" or something.

Can this be done, or am I wasting my time on something that's not really an issue?

The only real advantage this would give me would that I could put the x86 and x64 versions of the exe in the same directory, and have them load from the appropriate folder of dll's (bin32/ or bin64/, for instance).

I'm using C++ and MinGW-W64.

Upvotes: 1

Views: 2586

Answers (2)

Baruch Oxman
Baruch Oxman

Reputation: 1636

The following link explains the DLL Search order very nicely.
Quoting:

Before the system searches for a DLL, it checks the following:
If a DLL with the same module name is already loaded in memory, the system uses the loaded DLL, no matter which directory it is in. The system does not search for the DLL.
If the DLL is on the list of known DLLs for the version of Windows on which the application is running, the system uses its copy of the known DLL (and the known DLL's dependent DLLs, if any). The system does not search for the DLL.
For a list of known DLLs on the current system, see the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs.

You can refer to using SetDllDirectory to add custom search directories.

Dynamic-Link Library Search Order on MSDN

Upvotes: 1

Yochai Timmer
Yochai Timmer

Reputation: 49221

You can use the GNU linker's -rpath flag.

For example, this will search the local directory for dependent dlls: -Wl,-rpath,./

So specifically in your case you'd want: -Wl,-rpath,./bin

Upvotes: 1

Related Questions