简体   繁体   中英

How to link dll in visual studio?

To link static library I have to create .lib from .h and .c files. Then I add it to my project, put in Additional Dependencies and compile.

To use dll I need .dll and .lib. This .lib is the same as above or different? How to say to Visual studio to use .dll and .lin, not only .lib? I put .dll to project directory and nothing changed(.exe has the same size(should be less I think)).

When you compile a DLL project, you will get a DLL and a LIB file as output. The DLL contains the actual library code; the LIB file contains stubs for the exported functions that assist the linker in emitting code to call that DLL.

This is very different from the LIB file that you get when you compile a static library. That LIB file contains all of the object code that comprises the library. 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. The linker does the rest; it can tell from the LIB file what it is supposed to do.

Of course, you have to make sure it is the right LIB file. Having both projects (the DLL and the EXE) in the same solution will allow you to use project references, making the task essentially foolproof.

EDIT: You will of course not get a LIB file when you build a DLL unless the DLL exports functions. (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. 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. More information about that in my answer here . Alternatively, you can use a DEF file with an "EXPORTS" section .

Static LIB or (small) LIB with a DLL, both would require a header file (or explicit function declarations). However, the different between LIBs is:

  • (Dynamic) DLL : The .LIB is like a header file, and the .DLL file is like a .CPP file. Just like header, which contains declaration, .LIB contains import definitions. Just like CPP contains the definitions, the .DLL contains the actual code.
  • 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. Just like, the linker won't complain about missing implementation (.CPP in above case), the loader (OS) won't complain about missing DLL.

    DLL based libs are small, static libs are big (put the .H/.CPP analogy here).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM