1

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.

Community
  • 1
  • 1
Beginner
  • 5,277
  • 6
  • 34
  • 71
  • 1
    No need to write anything in C, `extern "C"` to get C linkage is enough. Also, most systems by now have a C++ ABI. Just make sure to avoid using anything not covered by it (Which means mostly some library-features). – Deduplicator Nov 20 '14 at 14:27
  • 1
    An arguably better solution is to keep the library as source code and compile for whatever platform on demand. – n. m. could be an AI Nov 20 '14 at 14:30
  • 1
    @Deduplicator I know that Solaris Studio has issues with some STL components. Would this work for methods like myMethod(std::vector data) ? – Beginner Nov 20 '14 at 14:30
  • @n.m. Solaris Studio does not compile my library since it does not compile third pary libs such as Apache Qpid – Beginner Nov 20 '14 at 14:31
  • 1
    Well that sucks. File bug reports with both vendors. – n. m. could be an AI Nov 20 '14 at 14:35
  • @Deduplicator AFAIK, only g++ and Intel compiler support the same C++ ABI. Solaris Studio lags behind in terms of standard conformance. – Maxim Egorushkin Nov 20 '14 at 15:06
  • @MaximYegorushkin: Naturally, if there are multiple ABIs on a platform (and you want to support multiple ABIs at once), things get more complicated, depending on how different they are. And compilers not following the platform ABI are always a special case. – Deduplicator Nov 20 '14 at 15:11
  • @Deduplicator I would not naturally pay for maintenance of multiple C++ ABIs. I would only pay for one C ABI in this case. – Maxim Egorushkin Nov 20 '14 at 15:14
  • @MaximYegorushkin: You mean only use what's identical in all of them, which might mean restricting yourself all the way down to the C ABI? Sure, that's what it might mean. – Deduplicator Nov 20 '14 at 15:16

1 Answers1

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.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • You were right to point out exceptions as a possible issue. I expected problems only when exceptions pass the c-layer, but it seems execptions are problematic in general? http://stackoverflow.com/questions/27490165/sun-studio-compiler-with-shared-libs-exceptions-do-not-work – Beginner Dec 16 '14 at 13:19