I'm about to start writing a library to support machine learning algorithms (decision trees, ANNs, Bayes nets, etc.), and I'm planning on making this a very general library. By this, I mean I want to be able to plug this module into as many languages as possible.

To clarify, here's why I've started writing it in C++ (Windows headers):

  • I know I can import C++ libraries into C#, and I've heard you can import C++ into Python. So, obviously, the library can be used in 3 languages, possibly 4 if I don't use system-specific headers.

  • Is there a language where I can cover a wider spread, weighted by popularity of languages? (For example, C# and C++ have pretty high weight to me because I use both frequently, Python less so.)

  • You can (or should be able to) call your library from code written in another language. Years ago I worked on a project that mixed FORTRAN, C/C++ and then one that mixed C# and C/C++ libraries.
    – ChrisF
    Commented Dec 23, 2011 at 10:39

3 Answers 3


If you are careful, you can write a quite general library with several bindings to other languages. For example both GTK (a graphical toolkit, coded in C, LGPL licensed) and Parma Polyhedra Library (a library handling numerical inegalities and abstractions, coded in C++, GPL licensed) have interfaces to several languages. Be sure to learn how to bind code to several languages & implementations (Ocaml, C++, Python, Haskell, Java, Lua, ...). Learn about memory management and garbage collection techniques.

I also suggest that you make your library free software, it is IMHO the best (and perhaps the only) way to make it widely used.

I strongly suggest that you make your library working on several platforms (from the beginning), like Windows, Linux, MacOSX. Don't make it Windows only!

Study the competitor's libraries. There already exist several machine learning libraries.

So the language to code your library is not the most important factor. And you could also make it a server, with published protocol to access it.

Perhaps chosing the language you know well is better. Otherwise, I might recommend Ocaml, C, C++, Java, ...

hints for C++

If coding in C++ consider using a recent C++11 compliant compiler and perhaps some cross-platform library framework like QtCore or Poco

  • Ocaml, C, C++ is good, specially OCaml, but what Java is doing here?
    – cnd
    Commented Dec 23, 2011 at 8:13
  • 1
    several machine learning libraries are written in Java... So you cannot say it is not relevant... Commented Dec 23, 2011 at 8:15
  • 1
    but importing Java to non-jvm application is nonsense imo.
    – cnd
    Commented Dec 23, 2011 at 8:54
  • 2
    @nCdy, JVM is embeddable (and you can link against GCJ binaries as well)
    – SK-logic
    Commented Dec 23, 2011 at 10:38
  • 1
    +1. Lua is a really good example here to the author: It uses only the ANSI C set of commands, so it is really easy to port and integrate on almost any language and almost any platform. If you are going to use C++, please, don't use Windows Headers. You will kill your library before birth - or will have a major headache porting it to MacOS / Linux later.
    – Machado
    Commented Dec 23, 2011 at 11:43

Generally speaking, you can write in whatever language you want, as long as you expose a C binding then virtually any language can bind to it.

  • Except most languages can't expose C binding. Anything with VM is totally out of question and not all compiled languages qualify either. In the end only C, C++ and probably FORTRAN and Ada are able to expose C interface.
    – Jan Hudec
    Commented Jul 19, 2014 at 15:23

If wide compatibility and portability are your main concerns, I think there are two serious options:

  • C/C++ - this will be most easily importable into most other languages, as nearly every language has some mechanism for calling C code. You can compile C/C++ code on pretty much any platform. Also if performance is a priority, it's nice to be able to code very close to the metal.
  • Java - it's the most popular language overall, with definitely the widest spread and portability of any of the major VM platforms, also is a very common choice for machine learning development. I suspect the majority of your potential library users will want to use Java. An additional advantage of pure Java is that it gives you binary (bytecode) portability so you don't even need to recompile for different platforms. The downside is that calling Java from other languages is a bit more tricky (one solution to this would be to provide a REST-style or socket API)

My personal recommendation would probably be to go for Java, because that's where the majority of your potential library users will be (research, enterprise software development, cloud startups etc.). If you want to make it really easy for these people to adopt your library, then a pure Java library will be much nicer than a set of native code bindings.

P.S. I do have quite a bit of experience in this space and have researched the options quite extensively, I'm currently writing a machine learning library on the JVM using Java + Clojure.

  • On server and desktop Java is portable. But on mobile platforms some either don't have Java at all or very poor. And of course binding to anything that itself does not run on JVM is difficult or impossible.
    – Jan Hudec
    Commented Jul 19, 2014 at 15:27
  • @Jan - true that certain mobile platforms have limitations. Though mobile platforms don't really have great portability for anything (JavaScript based solutions are probably your best bet...).
    – mikera
    Commented Jul 21, 2014 at 4:02
  • @Jan - But not true about binding to non-JVM systems. There are literally thousands of ways to for Java to interface to non-JVM code and systems. There is everything from JNA (native access to C libraries etc.) to socket-based solutions to REST APIs.
    – mikera
    Commented Jul 21, 2014 at 4:02
  • JNA is for binding C API to Java, not the other way around. The distinction is that if it has a Java part, it has to be started as Java application. As for inter-process communication, you can connect anything to anything with it, but for most situations where you want a library it the overhead just makes it impractical.
    – Jan Hudec
    Commented Jul 21, 2014 at 8:04
  • Jan - It's very much possible to use JNI to embed a JVM into a library written in C. The JVM is initialized in process with all the required Java runtime and used over the JNI interface by the C part of the library. Although, unless there is a good reason to do this, it does feel like a Mastiff sitting on a child's lap. Commented Jun 12, 2023 at 14:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.