1

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)).

Stark
  • 525
  • 7
  • 12
  • 1
    A DLL project generates an *import* library. Not the same thing as a static library, it is very small and only declares the functions that you exported. If you don't get a .lib file then you did not build it correctly or you forgot to export functions. – Hans Passant Jun 16 '16 at 15:02
  • DLL project creates only the .dll file. I can't find any .lib files – Stark Jun 16 '16 at 21:09

2 Answers2

5

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.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • After creating and compiling DLL project(from .c and .h files) I am getting only the .dll file. There is no the .lib. – Stark Jun 16 '16 at 17:16
  • And another question: how to use existing precompiled .dll without .lib file that I downloaded from a site which hosts library? Am I supposed to use LoadLibrary and GetProcAddress functions? – Stark Jun 16 '16 at 17:24
  • 1
    @stark It works for me. I did the following: add a new project to the solution, specifying the "Win32 Project" template. In the wizard, I picked "DLL". I annotated all of the functions I wish to be exported from the DLL with `__declspec(dllexport)`, and then I built the project. In the output folder, I get `MyDLL.dll`, `MyDLL.exp`, `MyDLL.ilk`, `MyDLL.lib`, and `MyDLL.pdb`. So I would guess that your problem is that you are not exporting any functions from the DLL. Which makes the DLL rather useless. – Cody Gray - on strike Jun 17 '16 at 05:52
  • 2
    See [here](http://stackoverflow.com/q/8863193/8863220#8863220) for more information on `__declspec(dllexport)`. As for the second question about downloaded DLLs, the download should contain a LIB file if it is intended to be used with Microsoft's compiler and linker. If it does not, yes, you will have to call the functions dynamically using LoadLibrary and GetProcAddress. Or you can manually create your own LIB file for the DLL. Get the list of exported functions using `dumpbin /exports`, put the names in a DEF file, and run it through the linker: `lib /def:MyDef.DEF /OUT:MyLib.lib` – Cody Gray - on strike Jun 17 '16 at 05:55
  • Oh thanks, i put __declspec(dllexport) in definition and all declaration of a function I need and it worked. I noticed that __declspec(dllimport) have to be with the object declaration(because object declaration with extern not working for dll), but function prototype works fine without __declspec(dllimport) . – Stark Jun 17 '16 at 16:30
2

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).

Community
  • 1
  • 1
Ajay
  • 18,086
  • 12
  • 59
  • 105