Programming in multiple languages - How?
Hi,
If one would want to write a program in multiple languages (say, using different languages for different sections of the problem that needs to be solved by the program), how should, or how could one achieve that? I am aware of linking compiled code written in different languages, but what about interpreted ones? What other methods are there, and what are the pitfalls of them? How elegant is simply writing two separate programs in different languages and making them interact (EDIT: at runtime I mean)? And no, my own specific problem is not about combining assembly with another language. I did some searches beforehand, inside LQ and in the interwebs, but I failed to find any satisfying answers. Btw, an older, related topic: http://www.linuxquestions.org/questi...guages-748548/ And here's ArthurSittler's reply when I asked him on email a week or two ago (yes, I asked his permission to copy-paste it): Quote:
|
Many languages (including many interpreted languages) include methods for coding functions in C to be called from those languages. Some also support call backs, so the C code can call functions in the interpreted language.
C++ allows you to declare functions matching any C interface, so any function that another language could call if it were written in C could be written in C++, and anything a C function can call a C++ function can call. C can call Fortran functions and vice versa. I expect the same is true of most other compiled languages. So if you wanted to combine one interpreted (or byte code or just in time compiled) language with C and several other compiled languages, that should be pretty easy. Where two languages don't have a defined way for one to call the other, you might need an interface layer in C. Some of the languages (such as Java) that normally use an implementation that is structurally equivalent to interpreted, are also available in a true compiled form. If you want to mix two different languages that are each structurally equivalent to interpreted, then you have a much tougher problem. The top level run time executable for each interpreted language is normally designed to be the foundation of the whole process. Fully compiled code in other languages can be brought in pretty easily as .so files. But another interpreter generally can't be brought in as a .so file. |
Quote:
If you chose a particularly eccentric combination, oooh, I don't know, Haskell and Forth (Cobol and APL would probably be entertaining, too) I think this would probably be untrodden ground and rather difficult Quote:
Quote:
But cf, Tcl/TK and other attempts to graft graphical toolkits on to languages not originally intended for that application, which is probably the most usual example of putting two languages together (followed by something interpreted and C/C++, to code functions to run more quickly). Quote:
|
Quote:
I.e. you've been given a correct advice to use IPC in order to make programs interact. Interpreted language can use IPC as well. You can also link programs written in different languages. like "C" and Fortran. You can bind functions written in, say, "C" to, say, a program in Perl (look for Perl Inline::C module). You can relatively easily combine pieces written in different language which ultimately work on the same virtual machine (if it's the case) - visit, for example, http://www.parrot.org/ . |
|
OCaml and C/C++: http://www.linux-nantes.org/~fmonnie...wrapping-c.php .
|
Quote:
Actually, what I'm after in this thread is receiving general answers to, basically, which are the common practices when one needs to combine two (or more) programming languages which don't have any already-existing interfacing solution? What can go wrong if one uses the "everything is a text file" approach? *(Actually, I do have a project in mind - only planning and daydreaming, mind you -, in which I'd use C and prolog (yes, I know about gprolog.h), I'm just curious how can one solve these kind of problems by not using interfacing. Actually, I may even write the whole program in prolog....) |
Quote:
|
All times are GMT -5. The time now is 09:28 PM. |