16

I currently have a console project which creates an .exe file; I want it to also create a .lib file so other projects, compiled as DLLs, would be able to call functions from the original project.

I know it is possible, but I couldn't find how to do that. How do I tell the linker to also link a .lib?

MoshiBin
  • 3,107
  • 2
  • 21
  • 23

5 Answers5

26

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.

iam3yal
  • 2,188
  • 4
  • 35
  • 44
10

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.

  • 1
    But right here I have a vcproj file that DOES end up with both an exe and a lib... Yet for the life of me I couldn't find what made it do that. http://svn.corecraft.org/aspire_mirror/trunk/win/VC90/hearthstone-world.vcproj – MoshiBin Apr 17 '10 at 11:51
  • @Spidey Presumably the project contains two sub-projects. –  Apr 17 '10 at 11:53
  • agreed. Make the exe project depend on the lib project and it'll get automatically recompiled when needed. – f4. Apr 17 '10 at 11:53
  • I see, one of the projects in the solution was actually a static library which the other used, just like you've suggested. Thanks! :) – MoshiBin Apr 17 '10 at 12:10
6

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?

Community
  • 1
  • 1
Gerardo Hernandez
  • 1,889
  • 1
  • 16
  • 20
  • This is correct w.r.t. the question in the title. However, how would this help with the other question in the body; "be able to call functions from the original [EXE] project"? – MSalters Mar 11 '11 at 13:35
  • You link against the generated .lib; the .exe behaves as a DLL. i.e. the symbols are loaded at runtime from the .exe. – Gerardo Hernandez Mar 28 '11 at 20:14
  • I tested what I wrote in the last comment. It sometimes work and sometimes don't with, a "Cannot initialize application (0xC0000005)" error. Using LoadLibrary and GetProcAddress I also end up with memory problems. So actually I do not know how to use the generated .lib properly. – Gerardo Hernandez Apr 07 '11 at 14:30
1

It is amazing how many contributors arrogantly insists on an answer that is wrong, when they simply don't know the answer.

To generate a .lib associated with your .exe place the following line in Pre-Link Event:

"$(VC_ExecutablePath_x86)\lib.exe" /out:"$(OutDir)$(ProjectName).lib" "$(IntermediateOutputPath)*.obj"
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
user6664090
  • 181
  • 2
  • 6
0

You don't "also link a lib", you create a static library project. The latter doesn't call the linker at all -- instead it compiles all your files with cl /c and combines the resulting .objs into a lib using lib.exe.

Alex Budovski
  • 17,947
  • 6
  • 53
  • 58