81

I want to have a single Visual Studio project that builds a DLL file and an import library (.lib) file. (An import library is a statically-linked library that takes care of loading that DLL file in other projects that use it).

So I went to Visual Studio C++ 2008 Express Edition, created a New Project of type Class Library, and set the "Configuration Type" to be "Dyanamic Library (.dll)".

But when I build the solution, the only relevant output file I see is a DLL file; I don't see any LIB file getting generated. I looked in the project directory and all subdirectories (Release and Debug).

I believe that it is possible to build a LIB and a DLL file at the same time because on the MSDN it says "The linker creates the import library when the DLL is built." Also, another user of this website is creating LIB and DLL files at the same time using Visual C++.

So how can I do it?

Community
  • 1
  • 1
David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • 4
    I just had this exact problem where my .lib file was not being generated. After googling through all info I could find about possible causes and carefully going through all the answers below I finally figured out that it was because my .cpp file didn't include my .h file. :( Just throwing it out here so that my comment could (with a small probability) help someone/myself if my stupid mistake is repeated in some distant future... – user1884905 Apr 11 '14 at 14:53

4 Answers4

87

Does your DLL project have any actual exports? If there are no exports, the linker will not generate an import library .lib file.

In the non-Express version of VS, the import libray name is specfied in the project settings here:

Configuration Properties/Linker/Advanced/Import Library

I assume it's the same in Express (if it even provides the ability to configure the name).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 7
    Thanks Michael, In my case lib was not getting generated because there were no exports. – Uday Nov 19 '09 at 06:09
  • 4
    OK, so what does this mean? i specified the import library on my dll project and it still didn't create the dll. I put it on the project that uses my DLL and it didn't cause it to be built either. I'm using Visual C++ 2010 Express – Thom Oct 24 '11 at 13:00
  • 3
    Could you clarify a bit on this? Specifying the import library alone does not seem to generate the .lib file. – Will Tice Sep 27 '12 at 03:02
  • 12
    @Will: do you have any functions marked with the `__declspec(dllexport)` attribute? – Michael Burr Sep 27 '12 at 06:09
  • 4
    Beware for beginners: An import library is not a static library, even if both are .lib. [More info on this answer](http://stackoverflow.com/a/11052481/2227298). – KrisWebDev Nov 11 '15 at 18:34
40

OK, so I found the answer from http://binglongx.wordpress.com/2009/01/26/visual-c-does-not-generate-lib-file-for-a-dll-project/ says that this problem was caused by not exporting any symbols and further instructs on how to export symbols to create the lib file. To do so, add the following code to your .h file for your DLL.

#ifdef BARNABY_EXPORTS
#define BARNABY_API __declspec(dllexport)
#else
#define BARNABY_API __declspec(dllimport)
#endif

Where BARNABY_EXPORTS and BARNABY_API are unique definitions for your project. Then, each function you export you simply precede by:

BARNABY_API int add(){
}

This problem could have been prevented either by clicking the Export Symbols box on the new project DLL Wizard or by voting yes for lobotomies for computer programmers.

Thom
  • 14,013
  • 25
  • 105
  • 185
  • Can you edit this so that it isn't an exact copy of your other answer? Making it apply to this question is a good starting point. Thanks. –  Oct 24 '11 at 18:07
  • Fixed, Will. I accidentally posted that answer to this page. I have now cleaned up my mess. Thanks for watchdogging me. – Thom Oct 24 '11 at 18:15
  • 2
    Dead link as of Sept 2017 – ToastyMallows Sep 11 '17 at 17:51
  • @ToastyMallows That's why I explained in detail my answer in case the link went away. – Thom Sep 12 '17 at 00:18
24

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.

Joe
  • 2,946
  • 18
  • 17
9

you also should specify def name in the project settings here:

Configuration > Properties/Input/Advanced/Module > Definition File

  • 6
    Located at `Configuration Properties`/`Linker`/`Input`/`Module Definition File` in VS2010 – Pakman Sep 09 '11 at 18:31
  • How does one create the definition file? What goes into it? – Thomas Matthews May 26 '13 at 19:05
  • Thank you ... this was driving me up the wall. – Richard Anthony Hein Jun 07 '13 at 11:50
  • 2
    @ThomasMatthews It's a file type if you go to Add New Item - Code - Module-Definition File (.def). Presumably it will add the definition file to the setting in the answer here, if added via VS. In my case, the .def pre-existed. – Richard Anthony Hein Jun 07 '13 at 11:53
  • @RichardHein: A [Module-Definition (.Def) File](https://msdn.microsoft.com/en-us/library/28d6s79h.aspx) *"provide[s] the linker with information about exports, attributes, and other information about the program to be linked.*" Among others it allows you to specify exports from a DLL (see [Exporting from a DLL Using DEF Files](https://msdn.microsoft.com/en-us/library/d91k01sh.aspx) for details). – IInspectable Jan 06 '17 at 16:50