How to Build an Import Library (.Lib) and a Dll in Visual C++

How do I build an import library (.lib) AND a DLL in Visual C++?

By selecting 'Class Library' you were accidentally telling it to make a .Net Library using the CLI (managed) extenstion of C++.

Instead, create a Win32 project, and in the Application Settings on the next page, choose 'DLL'.

You can also make an MFC DLL or ATL DLL from those library choices if you want to go that route, but it sounds like you don't.

How to import .lib into .dll?

Unfortunately, the VS linker doesn't have an option equivalent to ld's --whole-archive which can be used to include an entire library.

Your best bet is to unpack the library and link in the resulting object files. You can use the librarian (lib tool) for that. To list all members of the library, use lib /LIST. Object files have to be extracted one by one, using lib /EXTRACT:member.

How to generate an import library (LIB-file) from a DLL?

You can generate a DEF file using dumpbin /exports:

echo LIBRARY SQLITE3 > sqlite3.def
echo EXPORTS >> sqlite3.def
for /f "skip=19 tokens=4" %A in ('dumpbin /exports sqlite3.dll') do echo %A >> sqlite3.def

The librarian can use this DEF file to generate the LIB:

lib /def:sqlite3.def /out:sqlite3.lib /machine:x86

All of the filenames (sqlite3.dll, sqlite3.def, etc.) should be prepended with full paths.

Class library lib and dll files

You can have a library project or a DLL project. A DLL is good if it will be used by multiple exes. A lib is good if you want it to become part of an exe.

DLL projects generate both a DLL file and a lib file. The import lib file is very small and just contains a jump table so the exe can be compiled.

Linking library (.lib) and (.dll) in visual stucio c++ 2008

You do not need source files for use dll. All you need is .dll file. But in this case you should manually get address of every used function. Example for widows you can find here.

Getting address of every function is not very convenient. So you can use .lib file and header files (.h) (but not sources). In C/C++ Additional directories you should specify path to header files.

So you can configure your project:

  1. Linker Additional directories. (.lib folder)
  2. Additional libraries (.lib file)
  3. C/C++ Aditional directories. (Library headers path)


Related Topics



Leave a reply



Submit