Visual C++ does not generate .lib file for a DLL project

I had a DLL project in Visual Studio 2005. When built it should create a .dll file, and a .lib file, which is an import library file for the DLL user project to link against. After some code modifications, suddenly building the project did not yield the .lib file any more. The .dll file was fine, as rebuilding the project saw its file time update. I verified the linking options and the import library setting was correct. The compiling options were fine too.

Finally, it turned out that the code below caused the problem:

#ifdef ABC_DLL // abc.dll source code will define this macro before including this header
#define ABC_API //__declspec( dllimport )
#else
#define ABC_API __declspec( dllexport )
#endif
class ABC_API CAbc {…};

ABC_API attempts to set the calling convention of the abc.dll. Remember that the calling convention is not part of the C++ standard, so the actual meaning of ABC_API is only understood by the specific compiler, in this case, Microsoft C++ compiler. __declspec(dllimport) and __declspec(dllexport) are Microsoft’s calling convention settings to import from and export out of an DLL the symbols in it. These days few people use a module definition .DEF file, so __declspec(dllimport) and __declspec(dllexport) are almost the only way to specify the DLL’s exported objects. Some relavent information can be found in an earlier blog post.

Basically, in your source code of the DLL project, you want to make sure the objects compiled as __declspec(dllexport), such that these objects are correctly exported. These symbols will be put in an import library .lib file for others to use. For the user projects of the DLL, you want to make sure the code there see these objects from the DLL as __declspec(dllimport), such that the linker knows it should search an import library .lib file for these symbols.

In Visual Studio 2005, if a DLL project does not have any symbol to export, e.g., marked as __declspec(dllexport), it does not generate a .lib file, because there is no need. It still spits .dll file. The code above should be like below:

#ifndef ABC_DLL // abc.dll source code will define this macro before including this header
#define ABC_API __declspec( dllimport )
#else
#define ABC_API __declspec( dllexport )
#endif
class ABC_API CAbc {…};

The reason is pretty clear. It was only some typo without much thinking probably when busy modifying other stuff as well. To be more readable, it should be like below:

#ifdef ABC_DLL // abc.dll source code will define this macro before including this header
#define ABC_API __declspec( dllexport )
#else
#define ABC_API __declspec( dllimport )
#endif
class ABC_API CAbc {…};

 

About these ads

4 Responses to “Visual C++ does not generate .lib file for a DLL project”

  1. Thanks for the reminder. I did not even put any of the import/export silliness, since that’s only needed under MS-Windows! 8-P

    Nice post, I like the colors etc. makes it easy and fast to find the info you need.

    Alexis

  2. That was the solution for me as well
    thanks

Trackbacks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 44 other followers

%d bloggers like this: