How can I manage building library projects that create both a static library and a DLL?

I have a great visual studio solution with ~ 50 projects. There are configurations for StaticDebug, StaticRelease, Debug, and Release. Some libraries are needed in both dll and static lib form. To get them, we rebuild the solution with a different configuration. The Configuration Manager window is used to configure which projects to create, in which versions, static lib, dynamic dll, or both.

It can be quite difficult to manage, and it is a little annoying, you need to build a solution many times and choose configurations in the correct order. Static versions require the creation of non-static versions.

I am wondering, instead of this current scheme, it is easier to manage if for the projects that I need to create both a static lib library and dynamc, I created two projects. For example:

  • Corelib
  • Coredll

I could either make both of these projects reference all the same files and build them twice, or I wonder if it would be possible to build CoreLib and then get CoreDll to link it to generate a dll?

I think my question is, do you have any tips on how to structure your projects in this situation?

Thanks.

+6
c ++ dll visual-studio
source share
2 answers

Make a copy of the source project file in Explorer as CoreLib.vcxproj (in case other VS have checked the corresponding extension)

Add CoreLib.vcxproj as an existing project to your solution and save the solution.

Go to Properties-> Configuration Properties-> General from CoreLib .

Select all configurations (upper left corner).

Change the Configuration Type property to a static library .

Change the Target Extension property to .lib .

Add the Intermediate directory to the property, for example, \ Lib \ .

Go to Properties-> Configuration Properties-> C / C ++ β†’ Preprocessor

Select Debug Configuration (upper left corner).

Now edit the Preprocessor Definition property and change the line _USRDLL to _USRLIB

Select Release Configuration (upper left corner).

Now edit the Preprocessor Definition property and change the line _USRDLL to _USRLIB

Your header file should have something like the following:

#ifdef MyDll_EXPORTS #define MyDll_API __declspec(dllexport) #else #define MyDll_API __declspec(dllimport) #endif 

change it to the following:

 #ifdef MyDll_EXPORTS #ifdef _USRLIB #define MyDll_API #else #define MyDll_API __declspec(dllexport) #endif #else // Here must deploy your own logic whether static or dynamic linking is used. // read my comment below #define MyDll_API __declspec(dllimport) #endif 

Now your assembly will generate your original dll and import the lib as well as the new static lib!

+3
source share

Perhaps you could check which build system, such as CMake, produces for this case. I assume that it will generate two projects for each case. But by creating a solution this way, it could save you a lot of work.

+1
source share

All Articles