多个 Dll 通过“main”调用彼此的函数动态链接库
我正在构建一个游戏引擎(用 C++ 编写),它分为多个项目(在 Windows 上编译为单独的 DLL 和库,在 Linux 中编译为共享对象),其结构如下:
Main.exe
Engine.dll
Graphics Engine.dll Physics Engine.dll Sound Engine.dll ...dll
Main.exe 初始化一个新引擎,然后引擎创建图形、物理和声音引擎。这一切都工作正常,但我现在希望例如图形引擎运行声音引擎中存在的函数,但不是直接通过 Engine.dll 中的函数运行。
然而有一个问题。 Main包括Engine,Engine包括Graphics、Physics和Sound。如果我现在告诉图形、物理和声音包含引擎,并将引擎对象的引用传递给每个相应的“子引擎”,则存在循环依赖性问题...
我怎样才能让图形、物理和声音引擎与主引擎通信(因为目前这只是主引擎调用子引擎的所有功能的单向关系)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Engine.dll
中的抽象类(即带有纯虚函数)来公开接口。然后在Engine.dll
中实现该接口。然后创建具体此类的实例,并将该实例的引用/指针(作为抽象类引用/指针)传递给GraphicsEngine.dll
。现在,每当 GraphicsEngine 需要与 Engine 通信时,它都可以使用此实例。由于您仅使用抽象类,因此不需要将GraphicsEngine.dll
链接到Engine.dll
。所以不会出现循环依赖。You can expose an interface using a abstract class (i.e. with pure virtual functions) from the
Engine.dll
. Then implement this interface in theEngine.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 theGraphicsEngine.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 theGraphicsEngine.dll
to be linked toEngine.dll
. So there will be no circular dependency.