简体   繁体   English

如何更改应用程序使用的.dll搜索路径?

[英]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). 随着我的应用程序的增长,我发现自己使用了越来越多的第三方库,并且我的应用程序目录中的.dll数量也在增长(在撰写本文时为11)。 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. 我更希望能够将它们放在“ bin/ ”之类的东西中。

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). 这给我的唯一真正的好处是,我可以将exe的x86和x64版本放在同一目录中,并从dll的相应文件夹(例如bin32/bin64/ )中加载它们。

I'm using C++ and MinGW-W64. 我正在使用C ++和MinGW-W64。

You can use the GNU linker's -rpath flag. 您可以使用GNU链接器的-rpath标志。

For example, this will search the local directory for dependent dlls: -Wl,-rpath,./ 例如,这将在本地目录中搜索相关的dll: -Wl,-rpath,./

So specifically in your case you'd want: -Wl,-rpath,./bin 因此,具体而言,您需要: -Wl,-rpath,./bin

The following link explains the DLL Search order very nicely. 以下链接很好地说明了DLL搜索顺序。
Quoting: 引用:

Before the system searches for a DLL, it checks the following: 在系统搜索DLL之前,它会检查以下内容:
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. 如果内存中已经加载了具有相同模块名称的DLL,则系统将使用已加载的DLL,无论它位于哪个目录中。系统都不会搜索该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). 如果该DLL在运行该应用程序的Windows版本的已知DLL列表中,则系统使用其已知DLL的副本(以及已知DLL的从属DLL,如果有的话)。 The system does not search for the DLL. 系统不搜索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. 有关当前系统上的已知DLL的列表,请参见以下注册表项:HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ Control \\ Session Manager \\ KnownDLLs。

You can refer to using SetDllDirectory to add custom search directories. 您可以参考使用SetDllDirectory添加自定义搜索目录。

Dynamic-Link Library Search Order on MSDN MSDN上的动态链接库搜索顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM