Visual C++ 中的 DLL 引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/809948/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:23:16  来源:igfitidea点击:

DLL References in Visual C++

c++visual-c++dll

提问by Mark Stahler

I have had C++ experience but not MSVC.

我有 C++ 经验,但没有 MSVC。

What I am trying to do is incorporate a .dll from an open source project into my project. The code is available and I have built it. I have the .dll as well as the .lib which as I understand it is required for C++ projects.

我想要做的是将开源项目中的 .dll 合并到我的项目中。代码可用,我已经构建了它。我有 .dll 和 .lib,据我所知,C++ 项目需要它。

Now unfortunately there is no simple "Add Reference", drop my .dll into an include directory and add that to my solution. I have edited the project property pages, the C/C++ Additional Include Directories option as well as adding the .lib as an additional linker dependency. I have created an include directory for the dll and lib inside my solution tree.

不幸的是,现在没有简单的“添加引用”,将我的 .dll 放入包含目录并将其添加到我的解决方案中。我编辑了项目属性页、C/C++ 附加包含目录选项以及添加 .lib 作为附加链接器依赖项。我在我的解决方案树中为 dll 和 lib 创建了一个包含目录。

My problem is when I try to include the header files from the documentation, VS output spits out error messages. Now I realize that I am using the dll/lib combo and that the .h files are not present in my solution so how do I add the proper includes? I am using QT toolkit also which is working but how I add the other header / dll from the open source library eludes me.

我的问题是当我尝试包含文档中的头文件时,VS 输出会吐出错误消息。现在我意识到我正在使用 dll/lib 组合并且 .h 文件不存在于我的解决方案中,那么我如何添加正确的包含?我也在使用 QT 工具包,它也可以工作,但是我如何从开源库中添加其他头文件/dll 不知道。

Can someone please point me in the right direction.

有人可以指出我正确的方向。

回答by Aaron Cook

You need to do a couple of things to use the library:

你需要做一些事情来使用这个库:

  1. Make sure that you have both the *.lib and the *.dll from the library you want to use. If you don't have the *.lib, skip #2

  2. Put a reference to the *.lib in the project. Right click the project name in the Solution Explorer and then select Configuration Properties->Linker->Input and put the name of the lib in the Additional Dependencies property.

  3. You have to make sure that VS can find the lib you just added so you have to go to the Tools menu and select Options... Then under Projects and Solutions select VC++ Directories,edit Library Directory option. From within here you can set the directory that contains your new lib by selecting the 'Library Files' in the 'Show Directories For:' drop down box. Just add the path to your lib file in the list of directories. If you dont have a lib you can omit this, but while your here you will also need to set the directory which contains your header files as well under the 'Include Files'. Do it the same way you added the lib.

  1. 确保您拥有要使用的库中的 *​​.lib 和 *.dll。如果您没有 *.lib,请跳过 #2

  2. 在项目中放置对 *.lib 的引用。在解决方案资源管理器中右键单击项目名称,然后选择配置属性->链接器->输入并将库的名称放在附加依赖项属性中。

  3. 您必须确保 VS 可以找到您刚刚添加的库,因此您必须转到“工具”菜单并选择“选项...”,然后在“项目和解决方案”下选择“VC++ 目录”,“编辑库目录”选项。从这里,您可以通过在“显示目录:”下拉框中选择“库文件”来设置包含新库的目录。只需在目录列表中添加 lib 文件的路径即可。如果你没有一个库,你可以省略这个,但是当你在这里的时候,你还需要在“包含文件”下设置包含你的头文件的目录。以与添加 lib 相同的方式执行此操作。

After doing this you should be good to go and can use your library. If you dont have a lib file you can still use the dll by importing it yourself. During your applications startup you can explicitly load the dll by calling LoadLibrary (see: http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspxfor more info)

完成此操作后,您应该很高兴并且可以使用您的图书馆。如果您没有 lib 文件,您仍然可以通过自己导入来使用 dll。在应用程序启动期间,您可以通过调用 LoadLibrary 显式加载 dll(有关详细信息,请参阅:http: //msdn.microsoft.com/en-us/library/ms684175(VS.85 ) .aspx

Cheers!

干杯!

EDIT

编辑

Remember to use #include < Foo.h > as opposed to #include "foo.h". The former searches the include path. The latter uses the local project files.

请记住使用 #include < Foo.h > 而不是 #include "foo.h"。前者搜索包含路径。后者使用本地项目文件。

回答by Dani van der Meer

The additional include directories are relative to the project dir. This is normally the dir where your project file, *.vcproj, is located. I guess that in your case you have to add just "include" to your include and library directories.

额外的包含目录是相对于项目目录的。这通常是您的项目文件 *.vcproj 所在的目录。我想在您的情况下,您只需将“包含”添加到您的包含和库目录中。

If you want to be sure what your project dir is, you can check the value of the $(ProjectDir) macro. To do that go to "C/C++ -> Additional Include Directories", press the "..." button and in the pop-up dialog press "Macros>>".

如果您想确定您的项目目录是什么,您可以检查 $(ProjectDir) 宏的值。为此,请转到“C/C++ -> Additional Include Directories”,按“...”按钮,然后在弹出对话框中按“Macros>>”。

回答by imaginaryboy

You mention adding the additional include directory (C/C++|General) and additional lib dependency (Linker|Input), but have you also added the additional library directory (Linker|General)?

您提到添加额外的包含目录 (C/C++|General) 和额外的库依赖项 (Linker|Input),但您是否还添加了额外的库目录 (Linker|General)?

Including a sample error message might also help people answer the question since it's not even clear if the error is during compilation or linking.

包含示例错误消息也可能帮助人们回答问题,因为它甚至不清楚错误是在编译还是链接期间。