Question

I have a C++ project that builds as an EXE. However I also need to build the project as a static library (.LIB file). Right now in order to get a complete build, I need to build it in one format, then open the project properties and under General > Project Defaults > Configuration Type select the other format, accept, and recompile. It's a real hassle. Is there some way of specifying both types? I haven't seen anything on MSDN on this.

I'm using Visual Studio 2012.

Was it helpful?

Solution

A common (and particularly sane) way to solve your problem is to have a solution containing two projects. The first project should be a library (statically or dynamically linked, it doesn't matter) that contains most of your code. The second project should be an executable that references the library you've created in your first project.

Do it this way and a single build of your solution will produce both the executable and the library.

OTHER TIPS

Create two projects, add the same set of source files to both.

It's a quite old thread, but for the posterity: in case your project have a minimalist main.cpp, you could build a library from the whole .obj generated by the build, except main.obj. A typical way would be to add this build on a pre-link event:

<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
[...]
  <PreLinkEvent>
    <Command>@ECHO ON
    "lib.exe" /NOLOGO /OUT:"$(OutDir)$(ProjectName).lib" "$(IntermediateOutputPath)*.obj"
    "lib.exe" /NOLOGO "$(OutDir)$(ProjectName).lib" /REMOVE:"$(IntermediateOutputPath)main.obj"</Command>
  </PreLinkEvent>
</ItemDefinitionGroup>

It was working with Visual Studio 2010 & 2019 (didn't test with the intermediates ones).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top