如果库中的公开结构发生更改,使用它的应用程序是否必须重新编译?

发布于 2024-11-15 07:12:14 字数 179 浏览 1 评论 0原文

有一个动态库,其公开的结构正在被更改,新的字段正在结构的末尾添加。

该结构的内存是从库内分配的,应用程序不会分配它。但是,应用程序可能会直接访问该结构的成员。

在这种情况下,应用程序是否需要使用库再次重新编译?

库可用于所有操作系统(Windows、Linux、Solaris、HP-UX 等)。

There is a dynamic library, whose exposed structure is being changed, new fields are being added at the end of the structure.

Memory for the structure is allocated from within the library and application doesn't allocate it.But, the application may be accessing members of the structure directly.

In this case is it required for the application to recompile again with the library?

Library is being used on all Operating Systems (Windows, Linux, Solaris, HP-UX etc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够 2024-11-22 07:12:14

一些库(例如 FFmpeg)会执行类似的操作。 FFmpeg 有一个 struct AVFrame,其 sizeof 不得由应用程序使用,因为新字段可能会仅在较小版本的情况下添加。该库提供了分配struct AVFrame的函数;这些函数不允许分配它们的数组。必须小心避免使用比应用程序链接到的旧的次要版本;并非所有共享库机制都支持次要版本。

我认为这有点难看,但如果应用程序不做不允许的事情,那应该没问题。一种更简洁的方法是保持需要扩展的结构的布局私有。

Some libraries such as FFmpeg do something like this. FFmpeg has a struct AVFrame whose sizeof must not be used by applications, as new fields may be added with only a minor version bump. The library provides functions to allocate struct AVFrame; these functions not allow allocating arrays of them. Care must be taken to avoid using an older minor version than the application was linked to; not all shared library mechanisms support minor versions.

I think this is a bit ugly, but if applications do not do the disallowed things it should be OK. A cleaner method is to keep the layout of structs that need to be extended private.

薄荷港 2024-11-22 07:12:14

由于您问题中的这种情况:

该结构的内存是从库内分配的,应用程序不会分配它。

您不需要重新编译该应用程序。人们说你需要这样做是完全错误的。

Due to this condition in your question:

Memory for the structure is allocated from within the library and application doesn't allocate it.

You do not need to recompile the application. The people saying you need to are simply wrong.

陌生 2024-11-22 07:12:14

如果结构的大小没有改变,则不需要重新编译代码。这就是为什么您经常在公共 API 中看到这样的结构:

struct something {
  int public_field1;
  float public_field2;
  char padding[256];
}

添加新字段时,它们会添加到 public_field2 之后,并且填充的大小会减小,以便总大小保持不变。

If the structure's size doesn't change, recompiling the code is not needed. That's why you often see structs like this in public APIs:

struct something {
  int public_field1;
  float public_field2;
  char padding[256];
}

When new fields are added, they are added after public_field2, and the size of the padding is decreased so that the total size remains constant.

柒七 2024-11-22 07:12:14

KDE 技术库有一篇关于二进制兼容性的好文章。

我想说的是,

如果结构是公开的,应该需要重新编译。如果该结构仅供内部使用,那就没关系。

The KDE techbase has a good article on binary compatibility.

http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C++

I'd say that if the structure is public it should need to recompile. If the structure was only for internal use, it wouldn't matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文