views:

167

answers:

5

I want to write a library which will be dynamically linked from other programs running on modern operating systems like Windows, Linux and OS/X (i.e. it will be deployed as a .dll or .so module).

What is the most appropriate language in that case? Should I stick with plain C? Or is C++ also ok?

A: 

I'd say C is the most predictably portable, but C++ is doable.

Nosredna
+10  A: 

You can use either C or C++ for the implementation, but I would recommend to define the interface in pure C. It will be much easier to integrate.

Igor Krivokon
Also gives people consuming the libraries the freedom to chose what works best for them.
ojblass
Yeah. Few people object to the idea of OpenGL ES's straight C interface. But an Objective-C programmer might not dig a C++ interface at all.
Nosredna
+1  A: 

I would also say that C is the lowest common denominator. You always have the option of writing a C++ wrapper to the core library if this integrates better with the calling application.

Dana the Sane
A: 

Consider the factor of lowest common denominator and making consumers of your libraries make the decisions that are best for them. The construct of extern c probably still confuses some people and you want your library to travel far and reach the widest audience. Definitely make the interfaces pure c. C++ is fine provided you avoid some of the darker corners (like STL). C is the most portable bar none. Creating libraries for all available platforms is no small feat so be sure to take a look here for some hints. You might also want to consider using autoconf and the like.

ojblass
People who don't grok `extern "C"` aren't the kind of C++ developers you'd want to cater to. They're the kind of people that will bug you with other trivial stuff instead of RTFM/RTFSO.// Using AUtoconf will mean your program becomes more portable to 20th century Unix machines but less portable to Windows. For most people, that means giving up 95% of the market in exchange for 0.01% of the market (not exaggerated), less so for server software.
MSalters
I cannot do anything but agree with your statements. Its a challenging problem that does not have an easy solution.
ojblass
+2  A: 

The difficulty with creating a C++ library distributed in binary form is that your customers - the users of the library - are typically constrained to use the same C++ compiler as you created the library with. This can be problematic if you want to keep up to date and they don't, or if they want to keep up to date and you don't. If you deal in source, this is less of an issue, as long as your C++ is portable enough to allow it to be used by all the compilers your customers use.

If the code may be used from C, I'd probably code to a C interface. Alternative, provide two interfaces - the native C++ interface and a C interface. But that's more work than just a C interface. On the other hand, there may be benefits from a C++ interface (perhaps using STL iterators, etc) and that could sway your decision.

Jonathan Leffler