views:

109

answers:

2

What is the correct and easy step by step way to have multiple build configurations in the same VS project, where the Solution also contain multiple projects? The projects would have different build configurations. Basically, I'm looking for something like project A with Dll Debug, Dll Release, Static (library) Debug, and Static (library) Release, project B with Debug & Release that build a .exe.

+3  A: 

It's not the way it works. If you give the library the option to either link the CRT statically or use the DLL version of the CRT, you'll have to have that same option on the EXE project as well. Mixing the options will typically produce a raft of linker errors. Even if you manage to avoid them, disaster will strike at runtime when the functions from the .lib use a different memory allocator from the functions in the EXE.

Using the "all of them" approach makes little sense. As long as you know that you'll only use static libraries and create a monolithic EXE blob then the static version of the CRT makes sense. Makes deploying your program easier. If you are at all contemplating using DLLs some day then only the DLL version of the CRT makes sense.

Hans Passant
I am looking for how libraries source offer one an option to build Dll or static libraries (or both). There might be a separate project that build say an example executable, or tests.
KTC
@KTC: well, that's fine. VS will only build the .libs if you pick a configuration that the EXE doesn't have.
Hans Passant
A: 

The easiest way to achieve this would be to create multiple projects that compile the same sources. You could have projects for each of the cases you want (DLL, Static Lib). You could then have multiple configurations for those projects so that they could generate multiple variations.

Project A (DLL)

  • "DebugStatic" Configuration - Debug, Static CRT -> MYLIBDS.DLL
  • "ReleaseStatic" Configuration - Release, Static CRT -> MYLIBS.DLL
  • "DebugDynamic" - Debug, DLL CRT -> MYLIBD.DLL
  • "ReleaseDynamic" - Release, DLL CRT -> MYLIB.DLL

Project B (LIB)

  • "DebugStatic" Configuration - Debug, Static CRT -> MYLIBDS.LIB
  • "ReleaseStatic" Configuration - Release, Static CRT -> MYLIBS.LIB
  • "DebugDynamic" - Debug, DLL CRT -> MYLIBD.LIB
  • "ReleaseDynamic" - Release, DLL CRT -> MYLIB.LIB

You could use the same approach to generate distinct binaries for Ansi vs. Unicode, x86 vs. x64, single threaded vs. multi-threaded, etc. Each project includes the same set of source files with conditionalized code blocks as needed. Then, you can add something like this into the header file associated with the library to make it easier to link in the appropriate version:

#ifdef _DEBUG
  #ifdef _DLL
    #pragma comment(lib, "MYLIBD.LIB")
  #else
    #pragma comment(lib, "MYLIBDS.LIB")
  #endif
#else
  #ifdef _DLL
    #pragma comment(lib, "MYLIB.LIB")
  #else
    #pragma comment(lib, "MYLIBS.LIB")
  #endif
#endif
Jim Lamb

related questions