简体   繁体   English

如何构建独立于编译器的C ++库(用于Solaris Studio和gcc)?

[英]How to build a compiler-independent C++ library (for Solaris Studio and gcc)?

I would like to extend my library, which currently compiles only using gcc, to be used by Solaris Studio as well. 我想扩展我的库(该库当前仅使用gcc进行编译)也可以由Solaris Studio使用。

My idea is to do the following: 我的想法是执行以下操作:

  1. Write wrapper functions in C, which expose the relevant parts of the interface with extern C linkage. 用C编写包装函数,使用外部C链接公开接口的相关部分。
  2. Then build this library using gcc. 然后使用gcc构建该库。 The resulting c-header and binary are compiler independent as there is no name mangling anymore. 生成的c头文件和二进制文件与编译器无关,因为不再存在名称混乱的问题。
  3. Include the c-header and link into the project compiled with Solaris Studio. 包括c头并链接到使用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. 只要您的库公开与平台ABI兼容的C API(C类型的大小和对齐方式,调用约定)并且不引发C ++异常,您就不会遇到使用其他编译器或语言链接库的麻烦。

You could also add a C++ header only wrapper for your C API to make it easily reusable from C++ and exception safe. 您还可以为您的C API添加仅C ++标头包装器,以使其可以轻松地从C ++重用并保证异常安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM