0

Let's say I have written a library containing many classes in C++. Obviously, I can call this library from C++ client programs.

But, now let's say I want to use another language for my client programs. Since my C++ library contains many classes, I don't want to redevelop them in the client language.

So, I would like to be able to call my C++ library classes from other languages.

Also, I want the solution to be portable across platforms. I don't want to be tied to one platform (such as .NET where any .NET language can call any other .NET language).

In the extreme case, this inter-language call feature would be required even from "managed" languages such as Java.

So, my question is: Is it possible to call a library function written in one language, from a program written in another language? If so, how?

SSteven
  • 189
  • The "only" in your question is the curx of the issue (I wouldn't describe it as only). The easiest way to go is to export "C" your interface and call it a day, if that will suffice. – Mael Jan 31 '18 at 09:25
  • The intention is not to call a C++ program from a C program. It is to call a program written in one language from any other language. Eg: to call a C++ program from Java. – SSteven Jan 31 '18 at 15:41

1 Answers1

2

My understanding of the general use of "Middleware" is a little different to your description as it is not primarily about allowing libraries written in one language to be called from another.

I think that what you are looking for is tools that allow cross language bindings to be generate so as to allow libraries written in one language C++ in this case to be used in multiple other languages. A couple of examples of such tools are Boost and Swig.

In both of the above tools you need to build your C++ libraries to run on your target platform and build an interface layer for the language(s) that you wish to provide bindings for but in the case of Boost as far as I can see you need to pay a lot of attention to how your library is constructed. SWIG on the other hand parses your interface (.h) files and produces bindings for the supported target language(s) which include Tcl, Python, Perl, Guile, Java, Ruby, PHP, C#, R, Go, etc.

Steve Barnes
  • 5,300
  • Adding language bindings for the target language looks like a promising approach and just what I am looking for. (i) Boost offers a category for "Inter-language support", but Python is the only language that can interface with C++, using this library. (ii) I don't know if Swig supports pre-compiled C++ headers (c) Java Native Interface (JNI) is a bridge between C++ and Java. – SSteven Jan 31 '18 at 15:52