How do I create both a .lib file and an .exe file in Visual C++?

It's not possible in general - static libraries and executables are completely different kinds of animal. The way to handle this situation is to create two projects - one for the library, which contains all the functionality. and one for the executable, which is a thin wrapper that simply calls functions in the library.


If any symbol in a Application (.exe) project is exported (e.g. with __declspec(dllexport) ), both the .exe and the .lib files will be generated See: Why does my Visual C++ .exe project build create .lib and .exp files?


Posting this just as a reference I know the original post was posted long time ago but this still applies to anyone who needs a solution to this problem.

Go to the project you want to make a .lib file for and follow these steps:

  1. Right click on the project.
  2. Select Properties.
  3. Select Build Events.
  4. Select Pre-Link Event .
  5. Finally in the Command Line paste this:

    @ECHO ON @ECHO "$(VC_ExecutablePath_x86)\lib.exe" /out:"$(OutDir)$(ProjectName).lib" "$(IntermediateOutputPath)*.obj" "$(VC_ExecutablePath_x86)\lib.exe" /out:"$(OutDir)$(ProjectName).lib" "$(IntermediateOutputPath)*.obj"

This will call the lib tool to generate the lib file out of the generated object files.