简体   繁体   English

拆分一个Visual Studio C ++项目:如何(几乎)自动做到?

[英]Split a Visual Studio C++ project : how to do it (almost) automatically?

I have a Visual studio 2015 C++ project that has references to a few 3rd-party free library. 我有一个Visual Studio 2015 C ++项目,该项目引用了一些第3方免费库。

Just to reduce compile time (currently 5 - 90 seconds), 只是为了减少编译时间(目前为5-90秒),
I want to split the project into many smaller projects. 我想将项目分成许多较小的项目。

In preparation, I have already created filters in the solution explorer. 在准备过程中,我已经在解决方案资源管理中创建了过滤器
The filters show a top-level structure of my project - divided into tier :- 筛选器显示了我的项目的顶级结构-分为 :-

Tier 1  - my core library, no reference to outside, rarely change
Tier 2a - my custom encapsulator for external library "A"
Tier 2b - my custom encapsulator for external library "B"
...
    note: all Tier2 #include Tier1 , rarely change
Tier 3  - business logic that rarely change, #include Tier1&2
Tier 4  - business logic that often change , #include Tier1&2&3

Question : How to split this project into many projects easily? 问题 :如何将该项目轻松拆分为多个项目?
eg almost easy as click a filter then select "extracted to a new project" 例如,几乎很简单,只需单击过滤器,然后选择“提取到新项目”

I have read some sources - their explanation suitable for how to create from scratch, not for an existing project. 我已经阅读了一些资料-它们的解释适用于如何从头开始创建,而不适用于现有项目。

It also seems that I have to add some lines for some individual files. 似乎我还必须为某些单独的文件添加一些行。

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

I have >100 files, so it is not very practical. 我有100多个文件,所以不是很实用。
I googled "split visual studio c++ project", but did not find many results. 我用谷歌搜索“拆分Visual Studio C ++项目”,但没有找到很多结果。

Relate question : not explain "how" 相关问题 :不解释“如何”
How do you split a Visual Studio Solution? 如何拆分Visual Studio解决方案?

External link : all has the line #define DECLDIR __declspec(dllexport) 外部链接 :全部具有#define DECLDIR __declspec(dllexport)行
http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855/DLL-Tutorial-For-Beginners.htm http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855/DLL-Tutorial-For-Beginners.htm
https://manski.net/2011/11/creating-a-dll-project/ https://manski.net/2011/11/creating-a-dll-project/

I would like to note first that if your lower tiers rarely change, whole compilation would rarely occur too. 我首先要指出,如果您的下层很少更改,那么整个编译也将很少发生。 Also note that there is no one click to rule them all solution. 还要注意,没有一键单击即可解决所有问题。

You're almost there. 你快到了。 If you've already grouped your project into separate library, the next step would be to actually turn them into dynamic library. 如果您已经将项目分组到单独的库中,那么下一步将是将它们实际转换为动态库。 This would speed up compilation greatly as long as no symbol are changed (implementation modification is fine). 只要不更改任何符号(实现修改就可以了),这将大大加快编译速度。 For example for Tier 1 project: 例如,对于Tier 1项目:

First, create a dynamic library project (DLL Project). 首先,创建一个动态库项目(DLL Project)。 Erase all source code which generated by VS. 清除VS生成的所有源代码。 Group all Tier 1 's source code (header and cpp/implementation file) to the project's directory. 将所有Tier 1的源代码(标头和cpp /实现文件)分组到项目目录。 Add them to the project. 将它们添加到项目中。

We can have multiple dynamic libraries for each header file, but Visual Studio is not the best IDE for such task (use technology like CMake or QBS). 我们可以为每个头文件提供多个动态库,但是Visual Studio并不是执行此类任务的最佳IDE(使用CMake或QBS之类的技术)。 To be safe, consolidate declarations into single header file (or at least some manageable number, don't forget to change the #include in implementation files). 为安全起见,请将声明合并到单个头文件中(或至少一些可管理的数目中,不要忘记在实现文件中更改#include )。 Add dynamic library macro to the header, eg tier1lib.h 将动态库宏添加到头,例如tier1lib.h

#ifdef DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

This macro would tell whether we're importing or exporting a symbol based on our build commands. 这个宏将告诉我们是否要根据构建命令导入或导出符号。

Now in tier1lib.h , mark which symbol that will be visible to other project. 现在在tier1lib.h ,标记对其他项目可见的符号。 In each function, add DECLDIR macro such as 在每个函数中,添加DECLDIR宏,例如

DECLDIR void foo();
// or, if you want to export/import whole class' public symbols
class DECLDIR Foo{};

Build it. 建立它。

Now in the main project, tell VS about where to find the dynamic library. 现在在主项目中,告诉VS关于在哪里可以找到动态库。 Add include path (where to find headers), library path (where to find *.lib file) and library filename (eg tier1lib.lib ). 添加包括路径(在何处可以找到标题),库路径(在何处可以找到*.lib文件)和库文件名(例如tier1lib.lib )。 Copy the resulting *.dll to the main project's output folder and Tier 1 compilation is now completely separated to the main project. 将生成的*.dll复制到主项目的输出文件夹, Tier 1编译现在已完全分离到主项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM