是“C”吗? dlopen mini HOWTO”编译动态加载的 C++ 的推荐技术;插件库?
通过插件。
我们指的是通过 vi dlopen()
加载的库及其通过 dlsym()
解析的符号(不是由运行时系统动态加载的标准分片库)。
参考 http://www.isotton.com/howtos/C++-dlopen-迷你指南/。该文档的最后更新时间是 2006 年。它建议使用 extern "C"
来防止函数名称的损坏,以便 dlsym
可以相对轻松地找到其函数。
这对于动态库仍然相关吗?在我的特定情况下,我尝试使用 libtool 在 OSX 上创建动态库。也许使用 __attribute__ ((constructor)) 更时髦和现代,我在发现推荐的做法方面几乎没有成功。
By Plugin.
We mean a library that is loaded vi dlopen()
and its symbols resolved via dlsym()
(not a standard shard library that is dynamically loaded by the runtime system).
Referring to http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/. The document was last updated in 2006. It recommends the use of extern "C"
to prevent mangling of function names, so that dlsym
can find its functions with relative ease.
Is this still relevant for dynamic libraries? In my particular case, I am trying to create a dynamic library on OSX using libtool. Perhaps using __attribute__ ((constructor))
is more hip and modern, I have had little success discovering the recommended practice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我非常确定
extern "C"
仍然是从 C++ 代码导出未损坏函数的最佳方式。跨多个平台使用它没有问题。I'm pretty sure
extern "C"
is still the best way to export unmangled functions from C++ code. Using it across several platforms without issues.运行时插件
如果您计划手动加载库
dlopen()
并使用dlsym()
来检索和解析函数/对象,那么使用外部“C”名称会变得更加困难更轻松。对于损坏的名字来说这是一种痛苦。因此,如果您想要更轻松的可移植性/可用性,请使用 C(外部“C”)接口。
但您应该考虑公开 C(extern“C”)接口的缺点。
这意味着您不能直接通过接口公开任何 C++ 对象。因此,您最初会失去很多功能,例如 RAII。 调用来弥补这一点
您应该通过编写额外的包装器调用来包装对 C 接口普通共享库的
编辑:原始答案:
最初的问题是关于共享库的(只有通过注释我们才发现它是关于插件共享库的)。
所以这些名字是难以管理的。
如果编译器/运行时正在解析符号,这不是问题。
您计划使用多个编译器吗?因为这是我能看到导出 C 接口的唯一原因(因为不同的 C++ 编译器通常使用不同的 ABI)。
如果您使用相同的编译器,那么为什么不使用 C++ 接口。
就我个人而言,在任何给定平台上我只使用一个编译器,并且我只使用该平台上的共享对象。如果编译器升级了,发生了更大的事情,我无论如何都会重新构建所有库。
A runtime plugin
If you plan to load a library manually
dlopen()
and usedlsym()
to retrieve and resolve functions/objects then using a extern "C" name becomes much easier. It is a pain for mangled names.Thus if you want easier portability/usabilty then use an C (extern "C") interface.
But you should consider the cons to exposing a C (extern "C") interface.
This means you can not expose any of your C++ objects directly via the interface. Thus you are loosing a lot of functionality like RAII initially. You should compensate for this by writing extra wrapper calls to wrap the calls to your C interface
Normal shared libraries
Edit: Original answer:
The original question was about shared libraries (only via comments did we find it was about plugin shared libraries).
So the names are unmanageable.
This is not a problem if the compiler/runtime are resolving the symbols.
Do you plan on using multiple compilers? as this is the only reason I can see for exporting a C interface (as different C++ compiler often use different ABI).
If you are using the same compiler then why not use the C++ interface.
Personally on any given platform I only use one compiler and I only use the shared objects on that platform. If the compiler is upgraded something bigger has happened I am re-build all my libraries anyway.