简体   繁体   English

如何使用在 dll 中的 exe 中实现的类

[英]How to use classes implemented in exe from dll

I am building a simple game engine and a game in it.我正在构建一个简单的游戏引擎和一个游戏。 The idea is to have most of the functionality and basic class hierarchy implemented in the engine which should be an exe and then to have a dll which will implement specific game given the tools the engine provides.这个想法是在引擎中实现大部分功能和基本类层次结构,它应该是一个 exe,然后有一个 dll,它将在给定引擎提供的工具的情况下实现特定的游戏。 Similar to id engines such as idTech4 .类似于idTech4等 id 引擎。

The engine would then load the dll, acquire an instance of the game, give the game pointers to whatever functions and data structures the game needs and call methods such as game->update();然后引擎将加载 dll,获取游戏实例,将游戏指针指向游戏所需的任何函数和数据结构,并调用诸如game->update(); on the game instance.在游戏实例上。 So far so good.到目前为止,一切都很好。

I would like to have things like basic classes like Entity, Transform, Vector3 etc. implemented in the engine.我希望在引擎中实现诸如 Entity、Transform、Vector3 等基本类之类的东西。 Then in the game what I want is to for example derive my character from Entity.然后在游戏中我想要的是例如从 Entity 派生我的角色。

class Character: public engine::Entity {...};

Things like this compile all right since I include the headers from the engine but they do not link since I do not use the obj files nor the cpp files form the engine.这样的事情编译正常,因为我包含了来自引擎的头文件,但它们没有链接,因为我不使用引擎的 obj 文件或 cpp 文件。 I know why this does not work.我知道为什么这不起作用。 As far as I know my (reasonable) options are:据我所知,我的(合理的)选择是:

  1. Make these classes header only - this way it will always compile everywhere.仅使这些类成为标头 - 这样它将始终在任何地方编译。

I don't want this since it would increase compile times, binary size etc.我不想要这个,因为它会增加编译时间、二进制大小等。

  1. Put the common functionality into a lib or a dll.将通用功能放入 lib 或 dll 中。

This would work, but a lib file would mean that I have the same classes compiled (linked) both in the exe (engine) and dll (game) - this seems sloppy.这可行,但lib文件意味着我在 exe(引擎)和 dll(游戏)中编译(链接)了相同的类——这看起来很草率。 Dll would work but it seems like a lot of work with all the exports and imports I would need to do to both the engine and game. Dll可以工作,但似乎我需要对引擎和游戏进行所有导出和导入的大量工作。 Also probably most importantly both lib and dll approach share common issue which is that during development I may end up moving various class a lot from and to them since the class hierarchy may change a lot etc.同样可能最重要的是,lib 和 dll 方法都有一个共同的问题,那就是在开发过程中,我可能最终会在它们之间移动很多类,因为类层次结构可能会发生很大变化等。

My question is, is there another way around this?我的问题是,还有另一种解决方法吗? For me this seems like a pretty useful feature to have a hierarchy of classes in exe usable from a dll.对我来说,这似乎是一个非常有用的功能,可以在 dll 中使用 exe 中的类层次结构。 I tried even very silly things such as making the linker use the .obj files generated during engine compilation, but it did not work unless I specified all .obj files one by one (I am using Visual Studio 2017).我什至尝试了一些非常愚蠢的事情,例如让链接器使用引擎编译期间生成的.obj文件,但它不起作用,除非我一一指定所有.obj文件(我使用的是 Visual Studio 2017)。

I do not want to give up the modularity of this design.我不想放弃这种设计的模块化。

Edit : As dlls are platform specific let's consider Windows (although I guess other platforms would be similar).编辑:由于 dll 是特定于平台的,让我们考虑 Windows(尽管我猜其他平台会类似)。

It sounds like you are wanting to utilize dynamic loading, but I think you might have things flipped around the wrong way.听起来你想利用动态加载,但我认为你可能把事情搞错了。 The exe usually has the game code and the .dll has the game engine logic and classes. exe 通常包含游戏代码, .dll包含游戏引擎逻辑和类。 You load the .dll in the .exe code and register different things with the engine so your game code gets updated and rendered.您在.exe代码中加载.dll并向引擎注册不同的东西,以便您的游戏代码得到更新和呈现。

I'm thinking now as I type this if there is a way to flip it, but it would require a paradigm shift in your game design logic where the game only provides update and render functions to the game engine or something.我在输入此内容时正在考虑是否有翻转它的方法,但这需要在您的游戏设计逻辑中进行范式转换,其中游戏仅向游戏引擎或其他内容提供更新和渲染功能。 That is really all you can rely on as the engine has to be generic enough to handle games.这就是您真正可以依赖的全部,因为引擎必须足够通用才能处理游戏。

If I think of anything ground breaking I'll post it here, but in the mean time here is some info on dynamic loading to help you get started.如果我想到任何突破性的东西,我会在这里发布,但与此同时,这里有一些关于动态加载的信息可以帮助您入门。 https://en.wikipedia.org/wiki/Dynamic_loading https://en.wikipedia.org/wiki/Dynamic_loading

Put the common functionality into a lib or a dll.

This would work, but a lib file would mean that I have the same classes compiled (linked) both in the exe and dll这可行,但是 lib 文件意味着我在 exe 和 dll 中编译(链接)了相同的类

nope, it wouldn't.不,它不会。 The classes would be compiled and available on the shared library (.so on linux, .dll on windows, .dylib on mac), and your executable would call that particular method, from the shared library.这些类将被编译并在共享库(Linux 上为 .so,Windows 上为 .dll,Mac 上为 .dylib)上可用,并且您的可执行文件将从共享库中调用特定方法。 it would not be compiled again in your executable.它不会在您的可执行文件中再次编译。

Take a look on all frameworks or libraries in C++ are implemented:查看 C++ 中所有框架或库的实现情况:

  • The public part is exposed on the headers公共部分暴露在标题上
    • __dllexport(true) on the classes / functions. __dllexport(true)在类/函数上。
  • The private part is hidden隐私部分被隐藏
  • Do not worry about increase of load times if you use shared library approach, it's negligible.如果您使用共享库方法,请不要担心加载时间的增加,它可以忽略不计。

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

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