I want to improve compile times in a large C++ code base littered with cyclic dependencies. I've decided to go with using mainly pure abstract interfaces to reduce cyclic dependencies and thereby be able to split the project into smaller modules.
ibar.h:
struct IBar {
IBar();
virtual ~IBar();
virtual void foo() = 0;
}
std::unique_ptr<IBar> createIBar();
bar.cpp:
#include "ibar.h"
class Bar : IBar {
Bar();
virtual ~Bar();
virtual void foo() {<do stuff>;}
}
Now my createIBar function needs to be defined somewhere. If I define it in bar.cpp, anyone using using ibar.h needs to link in bar.o which is what I'm trying to avoid. So I need some sort of factory which can work for clients only using this interface.
In our code base we are already employing a pattern where we create run-time initializers which instantiate derived classes to fulfill the duties of an interface. This is done by the build system identifying initializer functions based on a fixed pattern, and these are "linked" into the main application via extern int definitions so all instantiations are run when the application starts.
I could use this pattern to create a factory that creates Bars for clients only knowing about IBar, but I don't like it because it imposes extra duties on the build system which I'm trying to improve in the first place. Second, I would like to employ lazy loading dlls at a later stage and this pattern effectively kills that. Third, since this will be done with quite a lot of components, the list of factories and initializer calls would grow quite large after a while.
What other techniques exist that can take care of this use case?