C ++设置dll搜索位置 [英] C++ set dll search location

查看:107
本文介绍了C ++设置dll搜索位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用SDL开发C ++应用,该应用需要DLL才能工作。现在,它将仅在与程序相同的文件夹中找到DLL。如何在名为 libs的子目录中搜索程序。

I'm currently developing a C++ app with SDL, which requires DLLs to work. Right now, it will only find DLLs in the same folder as the program. How can I make the program search in a sub-directory named "libs".

推荐答案

如果使用加载时间链接,则您需要将DLL位于DLL搜索路径中。在此处记录:动态链接库搜索顺序 。通常,这将需要将DLL文件夹添加到 PATH 环境变量。现在,将文件夹添加到 PATH 环境变量是解决问题的重要方法。

If you use load time linking then you need for the DLL to be located in the DLL search path. That is documented here: Dynamic-Link Library Search Order. Typically this would require that you add the DLL folder to the PATH environment variable. Now, adding a folder to the PATH environment variable is heavy weight solution to a problem. You surely don't want to do that.

另一方面,如果您切换到运行时链接,则可以将DLL的完整路径传递给 LoadLibrary 。您可以调用 GetModuleFileName 查找可执行文件的文件名,然后拉出目录,并添加 \libs\MyDll.dll 。但是运行时链接的一大缺点是,您需要为导入的每个函数使用 GetProcAddress

On the other hand, if you switch to run time linking then you can pass the full path of your DLL to LoadLibrary. You can call GetModuleFileName to find the file name of the executable, and then pull out the directory, and add \libs\MyDll.dll. But the big downside of run time linking is that you need to use GetProcAddress for each function that you import.

这些选择都不是特别有吸引力。我的建议是更改您建议的设计。将可执行文件所需的所有DLL与可执行文件放在同一目录中。

Neither of these options is particularly attractive. My advice would be to change your proposed design. Put all the DLLs that the executable needs into the same directory as the executable.

这篇关于C ++设置dll搜索位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆