How do I create both a .lib file and an .exe file

2019-03-25 01:35发布

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?

5条回答
来,给爷笑一个
2楼-- · 2019-03-25 01:50

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.

查看更多
太酷不给撩
3楼-- · 2019-03-25 01:52

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-03-25 02:05

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.

查看更多
三岁会撩人
5楼-- · 2019-03-25 02:10

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?

查看更多
混吃等死
6楼-- · 2019-03-25 02:10

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"

查看更多
登录 后发表回答