如何在 Visual Studio 2012 C++ 项目中引用(动态库)DLL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13076318/
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
How to reference a (Dynamic Lib) DLL in a Visual Studio 2012 C++ project?
提问by Rushino
I made a dynamic library with compiled headers and compiled a DLL (using Visual Studio 2012).
我制作了一个带有编译头文件的动态库并编译了一个 DLL(使用 Visual Studio 2012)。
I also have a C++ project made also with Visual Studio 2012 which i want to reference the previous DLL generated.
我还有一个 C++ 项目也是用 Visual Studio 2012 制作的,我想引用以前生成的 DLL。
How to reference that (Dynamic Lib) DLL in my Visual Studio 2012 C++ project ? Does i also need to point the header file (.h) if i want to use the functions of the library ?
如何在我的 Visual Studio 2012 C++ 项目中引用那个(动态库)DLL?如果我想使用库的功能,是否还需要指向头文件 (.h)?
采纳答案by moswald
I find it easiest to reference the DLL project using the Framework and References
item in the project's properties dialog:
我发现使用Framework and References
项目属性对话框中的项目引用 DLL 项目最简单:
It looks like you're adding some weird .Net thing into your C++ native code, but it's also the easiest way for a native-code project to reference another.
看起来您在 C++ 本机代码中添加了一些奇怪的 .Net 内容,但这也是本机代码项目引用另一个项目的最简单方法。
If you want to call functions in that DLL, then yes, you probably want to include that DLL's header file.
如果您想调用该 DLL 中的函数,那么是的,您可能希望包含该 DLL 的头文件。
回答by AndersK
There are a couple of ways to do this but all have in commmon that you need to include the header in your project so that the functions are known since C++ doesn't have reflection.
有几种方法可以做到这一点,但都有一个共同点,即您需要在项目中包含头文件,以便知道函数,因为 C++ 没有反射。
you can skip linking statically and load the dll using LoadLibrary and getting the functions using GetProcAddress, this has the advantage that you can make the DLL optional. If it is not there you could just avoid calling it or give some user friendly error message.
you can statically link to the dll, this means adding the .lib file of the dll to your project, there are a few ways to do this, the old school way is to add it in properties of your solution/linker/input/additional dependencies and then make sure the dll is in the exe search path. You will get an error message if it is not found.
您可以跳过静态链接并使用 LoadLibrary 加载 dll 并使用 GetProcAddress 获取函数,这具有可以使 DLL 可选的优点。如果它不存在,您可以避免调用它或提供一些用户友好的错误消息。
您可以静态链接到 dll,这意味着将 dll 的 .lib 文件添加到您的项目中,有几种方法可以做到这一点,老派的方法是将它添加到您的解决方案/链接器/输入/附加的属性中依赖项,然后确保 dll 在 exe 搜索路径中。如果未找到,您将收到一条错误消息。
回答by jonhkr
You can see the documentation here: http://msdn.microsoft.com/en-us/library/ms235636(v=vs.80).aspx(Creating and Using a Dynamic Link Library)
您可以在此处查看文档:http: //msdn.microsoft.com/en-us/library/ms235636(v = vs.80).aspx(创建和使用动态链接库)
回答by Wesam Na
I can give you an answer about how to add dll in VS 2010
我可以给你一个关于如何在 VS 2010 中添加 dll 的答案
steps: right click on solution explorer, properties , configuration properties, General, output directory. here press on right arrow and select brows. brows to DLL directory.
步骤:右键单击解决方案资源管理器,属性,配置属性,常规,输出目录。在这里按向右箭头并选择眉毛。浏览到 DLL 目录。
thats all.
就这样。