0

I have written a C++ library to do some numerical analysis. Is there a programmatic advantage to include a C API interface to the library in addition to the C++ API?

Is this isn't an appropriate question for stackoverflow I can delete it.

rhody
  • 2,274
  • 2
  • 22
  • 40
  • 3
    There is if your users will all be working in C. – Oliver Charlesworth Apr 20 '14 at 16:40
  • If you won't be the only one using this library, you'll probably want to provide a C API [for compatibility's sake](http://stackoverflow.com/questions/22797418/how-do-i-safely-pass-objects-especially-stl-objects-to-and-from-a-dll). As Matthieu's answer says, you cannot safely use a C++ library in a program built with a different compiler or compiler version than the library. – cf- Apr 20 '14 at 18:27

3 Answers3

4

What has C over C++: a stable well-defined ABI.

There are multiple C++ ABIs (MSVC's and Itanium being the foremost) and each Standard Library implementation (Dirkumware, libstd++ or libc++ for examples) is incompatible with the others. Therefore, the only way for C++ code to correctly link with C++ code is to be compiled with the same compiler (or with compilers sharing the same ABI) and above the same Standard Library implementation.

C, however, is different. C compiled by gcc integrates smoothly with C compiled clang... but it goes well beyond. It integrates smoothly with C++, Python, Haskell, Java, Rust, Lua, Perl, ... most if not all "serious" languages can interact with C more or less smoothly.

So, what is the benefit of a C API over a C++ ? Smooth integration with virtually any programming language under the sun.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Is there a disadvantage to "wrapping" a C++ api in C so that you get that compatibility and still have C++? – Brandon Apr 20 '14 at 21:47
2

Yes, there is one: you need a C API if you would like to call your functions from C code. Unlike the C API, which can called also by your C++ code (extern "C" { ... }), this is not true for C++ API, which can't be call by your C code.

AntiClimacus
  • 1,380
  • 7
  • 22
  • Also, decide on acceptability of extra indirection in the C API. If you have a C and a C++ interface, consider making the C++-class be inline callers of the C API instead of the other way around to avoid indirections. (Tweaking the calling conventions for C++ member functions to be identical to the corresponding C function left as an exercise for the reader) – Deduplicator Apr 20 '14 at 17:10
0

No there is no programmatic advantage to include a C API.

Guess it is down to marketing and what your customers want.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • What if someone wants to interface your library to a system which can't link to a C++ API, eg Julia, Object Pascal, FORTRAN 77 (and where there is no SWIG support) – rhody Apr 20 '14 at 16:41
  • 1
    @rhody Hence "what your customers want" in the answer. – JBentley Apr 20 '14 at 16:42
  • @rhody - i.e "what your customers want"! – Ed Heal Apr 20 '14 at 16:43
  • I'm looking for technical reasons not marketing ones, also why would a client want access to a C API, hence I added the qualifier programmatic. – rhody Apr 20 '14 at 16:44
  • Which is addressed in the first part of @EdHeal's answer. There is no *technical* advantage; it just makes the API more interoperable. – Lilshieste Apr 20 '14 at 17:42
  • Making something more interoperable is a technical issue. – rhody Apr 20 '14 at 18:38
  • Is not "what your customers want" the answer to any question whatsoever about a feature of a library? You really want to provide whichever language API your customers are using... Will they be linking the code to other C++ code or something else? – Vality Apr 20 '14 at 18:55
  • That's why I am asking, should I create a C API to supplement the C++ interface or not. From the comments it would seem there is a technical advantage to developers to have access to a C API, it makes it easier for developers to adapt the library to their needs. I should add that the library is for a large community of potential users, flexibility seems a worthwhile goal. The reason I went to SO was to canvas other suggestions why a C API might be technically useful in general. Interoperability seems to be a key advantage. – rhody Apr 20 '14 at 18:58
  • @rhody - Are those users using C or C++, fortran, ... I do not know. Perhaps better asking the people that would/are using it for their input. – Ed Heal Apr 20 '14 at 19:04