gpt4 book ai didi

c++ - 您如何向插件库公开功能?

转载 作者:太空狗 更新时间:2023-10-29 21:27:46 24 4
gpt4 key购买 nike

如果我有一个程序 foo.exe 并且它在运行时加载了一个插件 bar.dll,我如何让 bar.dll 中的代码使用 foo.exe 中的类? bar.dll 是否可以从 foo.exe 派生类?另外,该解决方案是否是跨平台的?

编辑:这是我正在尝试做的更多事情:


//foo.exe
class Node {
public:
void SetName(const string& n) { ... }
const string& GetName() { ... }
};

//bar.dll
class TransformNode : public Node {
public:
void DoSomething() {
SetName(...); //basically, can I inherit functionality from foo.exe (SetName and GetName)
//and reuse the code in a derived class in bar.dll?
}
};

最佳答案

为了让 bar.dll 使用来自 foo.exe 的类,您需要将一个带有虚函数的类的实例从 foo.exe 传递给 bar.dll。 Bar.dll 将调用来自 foo.exe 的虚函数。


是的,bar.dll 可以将派生类传递回 foo.exe,然后让 foo.exe 调用所述类的虚函数。


此解决方案将是跨平台的,但您将遇到重大问题,以确保您不会破坏共享库和 exe 之间的 abi。即使是最简单的更改也很容易破坏 abi。

This很好地介绍了可以破坏 abi 的因素。

关于c++ - 您如何向插件库公开功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8622854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com