我如何管理生成静态库和DLL的构建库项目? [英] How can I manage building library projects that produce both a static lib and a dll?

查看:358
本文介绍了我如何管理生成静态库和DLL的构建库项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的视觉工作室解决方案与〜50个项目。有StaticDebug,StaticRelease,Debug和Release的配置。在dll和静态lib形式中都需要一些库。要得到它们,我们用不同的配置重建解决方案。配置管理器窗口用于设置哪些项目需要构建在哪些flavor,静态库,动态dll或两者。



这可以通过相当棘手的管理,它是一个有点恼人,必须多次构建解决方案,并按正确的顺序选择配置。静态版本需要在非静态版本之前构建。



我想知道,而不是当前的方案,可能更容易管理,如果,对于我需要的项目产生一个静态lib和dynamc dll,我创建了两个项目。例如:




  • CoreLib

  • CoreDll



我可以让这两个项目引用所有相同的文件,并构建它们两次,或者我想知道,是否可以构建CoreLib,然后获得CoreDll链接到生成dll?



我想我的问题是,你在这种情况下如何构建你的项目有任何建议吗?



感谢。

解决方案

在explorer中将原始项目文件复制为 CoreLib.vcxproj (在其他VS检查适当扩展的情况下)



将CoreLib.vcxproj作为现有项目添加到您的解决方案并保存您的解决方案。



转到 CoreLib 属性 - >配置属性 - >常规



选择所有配置(左上角)。



将属性配置类型<



目标扩展属性更改为 .lib b

附加到属性中级目录,例如 \Lib \



转到属性 - >配置属性 - > C / C ++ - >预处理程序



选择调试配置

现在编辑属性预处理器定义,将 _USRDLL 行更改为 _USRLIB



选择发布配置(左上角)。

编辑属性预处理程序定义,并将 _USRDLL 行更改为 _USRLIB



头文件,你应该有如下:

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

将其更改为以下内容:

  #ifdef MyDll_EXPORTS 
#ifdef _USRLIB
#define MyDll_API
#else
#define MyDll_API __declspec(dllexport)
#endif
#else
//这里必须部署您自己的逻辑,不管是使用静态链接还是动态链接。
//读下面的注释
#define MyDll_API __declspec(dllimport)
#endif


b $ b

现在你的Build生成你的原始dll和导入lib以及一个新的静态lib!


I've got a large 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 setup which projects need to build in which flavours, static lib, dynamic dll or both.

This can by quite tricky to manage and it's a bit annoying to have to build the solution multiple times and select the configurations in the right order. Static versions need building before non-static versions.

I'm wondering, instead of this current scheme, might it be simpler to manage if, for the projects I needed to produce both a static lib and dynamc dll, I created two projects. Eg:

  • CoreLib
  • CoreDll

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

I guess my question is, do you have any advice on how to structure your projects in this kind of situation?

Thanks.

解决方案

Make a copy your original project file in explorer as CoreLib.vcxproj (in case of other VS check the appropiate extension)

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

Go to the properties->Configuration Properties->General of CoreLib.

Select all Configurations (left upper corner).

Change property Configuration type to static library.

Change property Target Extension to .lib .

Append to property Intermediate directory for example \Lib\ .

Go to the properties->Configuration Properties->C/C++->Preporcessor

Select Debug Configuration (left upper corner).

Now edit the property Preprocessor Definitions and change the line _USRDLL into _USRLIB

Select Release Configuration (left upper corner).

Now edit the property Preprocessor Definitions and change the line _USRDLL into _USRLIB

In your header-file you should have something like the following:

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

change it into something like 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 Build generates your original dll and import lib as well as a new static lib !

这篇关于我如何管理生成静态库和DLL的构建库项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆