如果库中的公开结构发生更改,使用它的应用程序是否必须重新编译?
有一个动态库,其公开的结构正在被更改,新的字段正在结构的末尾添加。
该结构的内存是从库内分配的,应用程序不会分配它。但是,应用程序可能会直接访问该结构的成员。
在这种情况下,应用程序是否需要使用库再次重新编译?
库可用于所有操作系统(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些库(例如 FFmpeg)会执行类似的操作。 FFmpeg 有一个
struct AVFrame
,其sizeof
不得由应用程序使用,因为新字段可能会仅在较小版本的情况下添加。该库提供了分配struct AVFrame
的函数;这些函数不允许分配它们的数组。必须小心避免使用比应用程序链接到的旧的次要版本;并非所有共享库机制都支持次要版本。我认为这有点难看,但如果应用程序不做不允许的事情,那应该没问题。一种更简洁的方法是保持需要扩展的结构的布局私有。
Some libraries such as FFmpeg do something like this. FFmpeg has a
struct AVFrame
whosesizeof
must not be used by applications, as new fields may be added with only a minor version bump. The library provides functions to allocatestruct 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.
由于您问题中的这种情况:
您不需要重新编译该应用程序。人们说你需要这样做是完全错误的。
Due to this condition in your question:
You do not need to recompile the application. The people saying you need to are simply wrong.
如果结构的大小没有改变,则不需要重新编译代码。这就是为什么您经常在公共 API 中看到这样的结构:
添加新字段时,它们会添加到
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:
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.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.