Search code examples
c++dllobjectlibrariescircular-dependency

Multiple Dll's calling each others functions though "main" dll


I am building a game engine (in c++) which is split up into multiple projects (compiling to separate dll's and libs on windows and shared objects in linux ) the structure is as follows:

                                    Main.exe

                                   Engine.dll

          Graphics Engine.dll Physics Engine.dll Sound Engine.dll ...dll

The Main.exe initializes a new Engine then the engine creates the graphics, physics and sound engines. This all works fine however I now want for example the Graphics engine to run functions present in the sound engine but not directly instead via a function in the Engine.dll.

There is a problem however. Main includes Engine, and Engine includes Graphics, Physics and Sound. If I now tell Graphics, Physics and sound to include Engine and also pass a reference of the engine object to each respective "sub-engine" there is a problem with circular dependencies...

How could I get Graphics, Physics and Sound engine to communicate to the Main engine (as at the moment it is only the one way relationship where the Main engine calls all of the function of the sub engines)?


Solution

  • You can expose an interface using a abstract class (i.e. with pure virtual functions) from the Engine.dll. Then implement this interface in the Engine.dll. Then create an instance of the concrete this class and pass the reference/pointer of this instance (as a abstract class reference/pointer) to the GraphicsEngine.dll. Now, whenever GraphicsEngine needs to communicate with Engine it can use this instance. Since you are using just the abstract class this doesn't require the GraphicsEngine.dll to be linked to Engine.dll. So there will be no circular dependency.