简体   繁体   English

如何在Visual Studio中链接DLL?

[英]How to link dll in visual studio?

To link static library I have to create .lib from .h and .c files. 要链接静态库,我必须从.h和.c文件创建.lib。 Then I add it to my project, put in Additional Dependencies and compile. 然后,将其添加到我的项目中,放入“其他依赖项”并进行编译。

To use dll I need .dll and .lib. 要使用dll,我需要.dll和.lib。 This .lib is the same as above or different? 该.lib与上述相同还是不同? How to say to Visual studio to use .dll and .lin, not only .lib? 如何对Visual Studio说使用.dll和.lin而不是.lib? I put .dll to project directory and nothing changed(.exe has the same size(should be less I think)). 我将.dll放到项目目录中,并且没有任何变化(.exe的大小相同(我认为应该更少))。

When you compile a DLL project, you will get a DLL and a LIB file as output. 编译DLL项目时,您将获得一个DLL和一个LIB文件作为输出。 The DLL contains the actual library code; DLL包含实际的库代码; the LIB file contains stubs for the exported functions that assist the linker in emitting code to call that DLL. LIB文件包含导出函数的存根,这些存根帮助链接器发出代码以调用该DLL。

This is very different from the LIB file that you get when you compile a static library. 这与编译静态库时获得的LIB文件非常不同。 That LIB file contains all of the object code that comprises the library. LIB文件包含构成该库的所有目标代码。 All such code gets linked directly into your executable when you build it—hence the "static" part of the name. 在构建可执行文件时,所有这些代码都直接链接到您的可执行文件中,因此,该名称的“静态”部分就会出现。

However, the actual manner of usage is very similar. 但是,实际使用方式非常相似。 Regardless of whether you are linking to a dynamic or static library, you point the linker (using "Additional Dependencies") to the LIB file. 无论链接到动态库还是静态库,都将链接器(使用“其他依赖项”)指向LIB文件。 The linker does the rest; 剩下的由链接器完成; it can tell from the LIB file what it is supposed to do. 它可以从LIB文件中得知应该执行的操作。

Of course, you have to make sure it is the right LIB file. 当然,您必须确保它是正确的LIB文件。 Having both projects (the DLL and the EXE) in the same solution will allow you to use project references, making the task essentially foolproof. 在同一个解决方案中同时拥有两个项目(DLL和EXE)将使您能够使用项目引用,从而使该任务本质上是万无一失的。

EDIT: You will of course not get a LIB file when you build a DLL unless the DLL exports functions. 编辑:除非DLL导出功能,否则您在构建DLL时当然不会获得LIB文件。 (If it doesn't export any functions, there's nothing for a client of that DLL to call, so there's no reason for a LIB file!) The simplest way to arrange for functions to be exported from a DLL is to use the __declspec(dllexport) annotation. (如果它不导出任何函数,则该DLL的客户端没有任何调用,因此没有理由使用LIB文件!)安排要从DLL导出函数的最简单方法是使用__declspec(dllexport)注释。 If combined with a macro, you can arrange for it to resolve to __declspec(dllimport) on the consumer side, allowing you to use the same header file for both building the DLL and consuming it from an application. 如果与宏结合使用,则可以在使用者方面安排它解析为__declspec(dllimport) ,从而允许您使用相同的头文件来构建DLL并从应用程序中使用它。 More information about that in my answer here . 有关此的更多信息,请参见此处 Alternatively, you can use a DEF file with an "EXPORTS" section . 或者,您可以将DEF文件与“ EXPORTS”部分一起使用

Static LIB or (small) LIB with a DLL, both would require a header file (or explicit function declarations). 带有DLL的静态LIB或(小型)LIB,都需要一个头文件(或显式函数声明)。 However, the different between LIBs is: 但是,LIB之间的区别在于:

  • (Dynamic) DLL : The .LIB is like a header file, and the .DLL file is like a .CPP file. (动态)DLL :.LIB类似于头文件,而.DLL文件就像.CPP文件。 Just like header, which contains declaration, .LIB contains import definitions. 就像包含声明的标头一样,.LIB也包含导入定义。 Just like CPP contains the definitions, the .DLL contains the actual code. 就像CPP包含定义一样,.DLL也包含实际代码。
  • Static LIB : Just like whole class is implemented in .H (mostly in case of templates), and no attached .CPP file - this kind of LIB contains all of code for the program to work. 静态LIB :就像整个类都在.H中实现(大多数情况下是在模板的情况下)一样,并且没有附加的.CPP文件-这种LIB包含程序正常工作的所有代码。 Just like, the linker won't complain about missing implementation (.CPP in above case), the loader (OS) won't complain about missing DLL. 就像链接程序不会抱怨缺少实现(在上述情况下为.CPP)一样,加载程序(OS)也不会抱怨缺少DLL。

    DLL based libs are small, static libs are big (put the .H/.CPP analogy here). 基于DLL的库很小,而静态库则很大(在这里以.H / .CPP类推)。

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

相关问题 链接到Visual Studio中的特定.dll文件吗? - Link to a specific .dll file in Visual Studio? 如何在 Visual Studio 的同一个 C++ 解决方案中将 dll 项目正确链接到另一个项目? - How to correctly link a dll project to another project in the same C++ solution in Visual Studio? 如何将opencv和其他dll文件链接到Visual Studio 2013的输出exe - How to link opencv and other dll files to output exe of visual studio 2013 在Visual Studio 2012中,如何使用C ++ DLL链接资源文件(.rc) - In Visual Studio 2012, how can one link resource file(.rc) with c ++ dll 为什么Visual Studio 2015不链接到相关的dll项目 - Why doesn't Visual Studio 2015 link to a dependent dll project 无法在 Visual Studio 中链接 imm32.dll - Unable to link imm32.dll in Visual Studio 在Visual Studio 2013中将libcurl库静态链接到我的项目(一个dll) - Statically link the libcurl library to my project (a dll) in Visual Studio 2013 在Visual Studio dll项目中链接.lib并导出类/函数 - Link .lib in Visual Studio dll project and export classes/functions 如何在 Visual Studio 中设置依赖 DLL 路径? - How to set dependency DLL paths in Visual Studio? 如何在Visual Studio 2010中正确引用dll? - How to correctly reference a dll in Visual Studio 2010?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM