问题描述
我想扩展我的库,它目前只使用gcc进行编译,以供Solaris Studio使用。
我的想法是执行以下操作:
- 在C中编写包装函数,它通过 extern C 链接公开接口的相关部分。
- 然后使用gcc构建这个库。由此产生的c头文件和二进制文件与编译器无关,因为不再存在任何名称混乱。
- 将c头文件和链接包含到使用Solaris Studio编译的项目中。 $ b
问题:这是一种可行的方法还是有更好的解决方案?
注意:除了名称修改外,还要注意。
你的计划是正确的。只要你的库暴露了一个与平台ABI兼容的C API(C类型的大小和对齐,调用约定),并且不抛出C ++异常,你就不会去使用其他编译器或语言连接你的库。
你也可以为你的C API添加一个C ++头文件封装器,使它可以轻松地从C ++和异常安全中重用。
I would like to extend my library, which currently compiles only using gcc, to be used by Solaris Studio as well.
My idea is to do the following:
- Write wrapper functions in C, which expose the relevant parts of the interface with extern C linkage.
- Then build this library using gcc. The resulting c-header and binary are compiler independent as there is no name mangling anymore.
- Include the c-header and link into the project compiled with Solaris Studio.
Question: Is this a feasible approach or is there a better solution to this problem?
Note: Besides name mangling, also watch out for problems related to exception handling.
Your plan is correct.
As long as your library exposes a C API compatible with platform ABI (sizes and alignments of C types, calling conventions) and does not throw C++ exceptions you are not going to have troubles linking your library using other compilers or languages.
You could also add a C++ header only wrapper for your C API to make it easily reusable from C++ and exception safe.
这篇关于如何构建独立于编译器的C ++库(用于Solaris Studio和gcc)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!