C++ 为什么 Visual Studio 找不到我的 DLL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4953843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't Visual Studio find my DLL?
提问by brainydexter
In Visual Studio 2010, under VC++ Directories > Executable Directories
, I have specified the path to glew32d.dll
. However, when I run the executable, it still complains.
在 Visual Studio 2010 下VC++ Directories > Executable Directories
,我指定了glew32d.dll
. 但是,当我运行可执行文件时,它仍然会抱怨。
On the other hand, if I copy the DLL into the local folder and run the executable then, it doesn't complain.
另一方面,如果我将 DLL 复制到本地文件夹并运行可执行文件,它不会抱怨。
Can someone please tell me how to fix this? Also, why is Visual Studio not recognizing that path?
有人可以告诉我如何解决这个问题吗?另外,为什么 Visual Studio 无法识别该路径?
UpdateScenario: I currently use a template project which I use as a starter code for a lot of my projects. This template depends on glew32d.dll. I usually store all dependent dlls in a common bin folder. I was hoping to reference this folder and Visual studio could read the dlls from there, instead of me having to copy the dlls everytime. What would be a good way to handle this?
更新场景:我目前使用一个模板项目,我将其用作许多项目的入门代码。该模板依赖于glew32d.dll。我通常将所有依赖的 dll 存储在一个公共 bin 文件夹中。我希望引用这个文件夹,Visual Studio 可以从那里读取 dll,而不必每次都复制 dll。处理这个问题的好方法是什么?
回答by Cody Gray
Specifying the path to the DLL file in your project's settings does not ensure that your application will find the DLL at run-time. You only told Visual Studio how to find the files it needs. That has nothing to do with how the program finds what it needs, once built.
在您的项目设置中指定 DLL 文件的路径并不能确保您的应用程序将在运行时找到 DLL。您只告诉 Visual Studio 如何找到它需要的文件。这与程序如何在构建后找到它需要的东西无关。
Placing the DLL file into the same folder as the executable is by far the simplest solution. That's the default search pathfor dependencies, so you won't need to do anything special if you go that route.
To avoid having to do this manually each time, you can create a Post-Build Event for your project that will automatically copy the DLL into the appropriate directory after a build completes.
将 DLL 文件与可执行文件放在同一文件夹中是迄今为止最简单的解决方案。这是依赖项的默认搜索路径,因此如果您选择该路径,则无需执行任何特殊操作。
为了避免每次都手动执行此操作,您可以为您的项目创建一个构建后事件,它会在构建完成后自动将 DLL 复制到适当的目录中。
Alternatively, you could deploy the DLL to the Windows side-by-side cache, and add a manifest to your application that specifies the location.
或者,您可以将 DLL 部署到 Windows并行缓存,并将清单添加到指定位置的应用程序。
回答by Oleg
I've experienced same problem with same lib, found a solution here on SO:
我在同一个库中遇到了同样的问题,在 SO 上找到了解决方案:
Search MSDN for "How to: Set Environment Variables for Projects". (It's Project>Properties>Configuration Properties>Debugging "Environment" and "Merge Environment" properties for those who are in a rush.)
The syntax is NAME=VALUE and macros can be used (for example, $(OutDir)).
For example, to prepend C:\Windows\Temp to the PATH:
PATH=C:\WINDOWS\Temp;%PATH%
Similarly, to append $(TargetDir)\DLLS to the PATH:
PATH=%PATH%;$(TargetDir)\DLLS
在 MSDN 中搜索“如何:为项目设置环境变量”。(对于那些匆忙的人来说,它是项目>属性>配置属性>调试“环境”和“合并环境”属性。)
语法为 NAME=VALUE,可以使用宏(例如,$(OutDir))。
例如,要将 C:\Windows\Temp 添加到 PATH:
PATH=C:\WINDOWS\Temp;%PATH%
类似地,要将 $(TargetDir)\DLLS 附加到 PATH:
PATH=%PATH%;$(TargetDir)\DLLS
(answered by Multicollinearity here: How do I set a path in visual studio?
(此处由多重共线性回答:如何在 Visual Studio 中设置路径?
回答by waytofall
try "configuration properties -> debugging -> environment" and set the PATH variable in run-time
尝试“配置属性 -> 调试 -> 环境”并在运行时设置 PATH 变量
回答by may5694
To add to Oleg's answer:
添加到奥列格的答案:
I was able to find the DLL at runtime by appending Visual Studio's $(ExecutablePath)
to the PATH environment variable in Configuration Properties->Debugging. This macro is exactly what's defined in the Configuration Properties->VC++ Directories->Executable Directories field*, so if you have that setup to point to any DLLs you need, simply adding this to your PATH makes finding the DLLs at runtime easy!
通过将 Visual Studio 附加$(ExecutablePath)
到配置属性->调试中的 PATH 环境变量,我能够在运行时找到 DLL 。这个宏正是在 Configuration Properties->VC++ Directories->Executable Directories 字段中定义的内容*,所以如果你有这个设置来指向你需要的任何 DLL,只需将它添加到你的 PATH 中就可以轻松地在运行时找到 DLL!
* I actually don't know if the $(ExecutablePath)
macro uses the project's Executable Directories setting or the global Property Pages' Executable Directories setting. Since I have all of my libraries that I often use configured through the Property Pages, these directories show up as defaults for any new projects I create.
* 我实际上不知道$(ExecutablePath)
宏是使用项目的可执行目录设置还是全局属性页的可执行目录设置。由于我通过属性页配置了我经常使用的所有库,因此这些目录显示为我创建的任何新项目的默认值。