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

一世执手 提交于 2019-12-11 06:57:12

问题


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:

  1. Write wrapper functions in C, which expose the relevant parts of the interface with extern C linkage.
  2. Then build this library using gcc. The resulting c-header and binary are compiler independent as there is no name mangling anymore.
  3. 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.


回答1:


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.



来源:https://stackoverflow.com/questions/27041788/how-to-build-a-compiler-independent-c-library-for-solaris-studio-and-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!