I have 6 static library projects :-
- Math
- ECS : depends on Math
- Utility : depends on ECS
- Physics : depends on Utility
- Graphics : depends on Utility
- BaseGame : depends on Physics and Graphics
- Some game (.exe): depends on BaseGame
(The "depends" here is transitive e.g. BaseGame also depends on ECS.)
I succeeded in using 6 projects via "static libraries" technique.
Today, I heard that dynamic library can reduce compilation time (Let's not discuss whether this is true),
so I read these below links and successfully create a small demo.
- https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2019 (official)
- https://www.badprog.com/c-windows-creating-a-dynamic-link-library-dll (alternative)
- How do I build an import library (.lib) AND a DLL in Visual C++? (troubleshooting)
Here is some code in my test demo :-
#ifdef SomeName1_EXPORTS
#define SomeMacro1 __declspec(dllexport)
#else
#define SomeMacro1 __declspec(dllimport)
#endif
SomeMacro1 void someFunction(int someParam);
Now, it is exciting time to apply it to my real projects.
At the first step, I want to export all functions and classes of my 6 libraries.
I assume that I have to add SomeMacro1
(different for each project) to every function in all 6 projects (~100K lines), right?
That is a huge refactoring.
Are there any easier way? Do I miss something very important?
Other notes :-
- I want to switch my library projects back to static library easily (in case something go wrong).
- I prefer a cross platform solution. (e.g. no pervasive refactoring need if I want to run in Linux later)
- I prefer a solution that when I cut-and-paste a source file from one project to another, the cost of refactoring (in code) does not increase from normal.
(SomeMacro1
is currently specific to a project)
Similar question : How to convert a static library project into a dll project in VS2005
Bounty Reason
Thank Andriy Tylychko's answer that provides useful cautions and suggests that refactoring would be inevitably complicated, but I still believe there are some easy ways to refactor my projects.
Now, I wish to change my library projects to dynamic libraries. (faster compilation)
Then when I ship my product, I will convert them back to static library. (better performance)
Edit: Bounty awards to Robert Andrzejuk because of his link in comment.
(https://learn.microsoft.com/en-us/cpp/cpp/using-dllimport-and-dllexport-in-cpp-classes?view=vs-2019)
It may sound simple, but I have never known that I can __declspec(dllexport)
at class level.
Although that is not my dream, it makes a lot of things easier.