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

I would like to expand my library, which currently only compiles using gcc, which will also be used by Solaris Studio.

My idea is to do the following:

  • Write shell functions in C that expose the corresponding parts of the interface using extern C.
  • Then create this library with gcc. The resulting c-header and binary are compiler independent since the name no longer exists.
  • Include the c-header and link in a project compiled using Solaris Studio.

Question: Is this an acceptable approach or is there a better solution to this problem?


Note. In addition to managing names, also look at issues related to exception handling .

+1
source share
1 answer

Your plan is correct.

As long as your library provides a C API that is compatible with the ABI platform (C sizes and alignments causing conventions) and does not throw C ++ exceptions, you will not have problems linking your library using other compilers or languages.

You can also add only the C ++ header shell for your C API to make it easily reusable from C ++ and a safe exception.

+1
source

Source: https://habr.com/ru/post/1209135/


All Articles