gpt4 book ai didi

c++ - 通过虚拟接口(interface)类导出 C++ 类的可移植性

转载 作者:行者123 更新时间:2023-11-30 02:26:34 28 4
gpt4 key购买 nike

我想编写一个可以用作插件的库。该库是用 C++ 编写的,也应该在 C++ 代码中使用。我找到了 this article它描述了如何通过用户可见的纯虚拟接口(interface)结构导出 c++ 类。简而言之,代码如下所示:

struct VirtualInterface
{
virtual MyExportedFunction() = 0;
}

class MyInterfaceImplementer : public VirtualInterface
{
...
virtual MyExportedFunction(){...}
...
}

extern "C" MyAPI VirtualInterface* Factory(); // The only exported function

文章的作者在某些时候指出:

A hypothetical C++ compiler that fails to support COM efficiently is doomed to oblivion in the Windows market. That is why ,nowadays, exposing a C++ class from a DLL via an abstract interface will work reliably with every decent C++ compiler on the Windows platform.

我是否理解这种导出 C++ 类的方式依赖于编译器实现并且不能保证按标准工作?如果是,是否有一种可移植的方式,除了通过创建一个导出接口(interface)的每个函数的 C 接口(interface)?

最佳答案

您可以可移植地导出 C++ 类/接口(interface) on GNU/Linux and BSD because compilers support Itanium ABI :

Starting with GCC 3.2, GCC binary conventions for C++ are based on a written, vendor-neutral C++ ABI that was designed to be specific to 64-bit Itanium but also includes generic specifications that apply to any platform. This C++ ABI is also implemented by other compiler vendors on some platforms, notably GNU/Linux and BSD systems. We have tried hard to provide a stable ABI that will be compatible with future GCC releases, but it is possible that we will encounter problems that make this difficult. Such problems could include different interpretations of the C++ ABI by different vendors, bugs in the ABI, or bugs in the implementation of the ABI in different compilers. GCC’s -Wabi switch warns when G++ generates code that is probably not compatible with the C++ ABI.

但是,如果接口(interface)中公开了 C++ 标准库中的类,则 API 的实现和使用者必须使用相同的 C++ 库实现:

The C++ library used with a C++ compiler includes the Standard C++ Library, with functionality defined in the C++ Standard, plus language runtime support. The runtime support is included in a C++ ABI, but there is no formal ABI for the Standard C++ Library. Two implementations of that library are interoperable if one follows the de-facto ABI of the other and if they are both built with the same compiler, or with compilers that conform to the same ABI for C++ compiler and runtime support.

When G++ and another C++ compiler conform to the same C++ ABI, but the implementations of the Standard C++ Library that they normally use do not follow the same ABI for the Standard C++ Library, object files built with those compilers can be used in the same program only if they use the same C++ library. This requires specifying the location of the C++ library header files when invoking the compiler whose usual library is not being used.

由于上述支持 COM 的要求,这也可能适用于 Windows 编译器。但是析构函数有一个问题:COM 不使用析构函数,因此支持 COM 的两个编译器可能在将析构函数指针放在 v 表中的位置上有所不同。

关于c++ - 通过虚拟接口(interface)类导出 C++ 类的可移植性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42645041/

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