4

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?

asked Jun 22, 2009 at 0:56

5 Answers 5

14

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.

answered Jun 22, 2009 at 0:58
Sign up to request clarification or add additional context in comments.

2 Comments

Also gives people consuming the libraries the freedom to chose what works best for them.
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.
2

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.

answered Jun 22, 2009 at 1:13

Comments

1

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.

answered Jun 22, 2009 at 1:01

Comments

0

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

answered Jun 22, 2009 at 0:58

Comments

0

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.

answered Jun 22, 2009 at 1:11

3 Comments

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.
I cannot do anything but agree with your statements. Its a challenging problem that does not have an easy solution.
Using CMake in place of autoconf will give you the same set of introspection and configuration + more. Including Windows. And as a personal opinion, in a much cleaner way.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.