views:

139

answers:

7

Is it possible to compile multiple languages together in order to get the best of the different languages.

+4  A: 

Yes, it's possible, but a lot depends on the specific languages. For example, calling C functions or C++ classes from Python is done routinely.

Mark Ransom
but what about making it language independent. I know .NET allows language interoperability via IL, but why we cant combine different languages which can be natively compiled. One of the solution Alex Martelli gave is separating them in different files and compiling them separately but is it possible to create a compiler which makes use of all compilers to compile different languages source present in the same file
Xinus
Xinus: writing a compiler which understands multiple languages mixed together is certainly possible, if you can resolve interoperability ambiguities and other issues. But it's hard enough to write a great compiler for *one* language which does everything you want to do, let alone solve these extra issues. Plus language designers will always borrow what they consider useful features from other languages, and can almost always work them into that language much easier than it would be to try supporting the whole of many different languages.
Roger Pate
+5  A: 

It's definitely possible to link them together (if suitably programmed) after compiling them separately, if the compilers and linkers are all compatible. For example:

g77 -c one.f
gcc -c two.c
gcc -o together one.o two.o

this compiles a Fortran file, then a C file, then links them together in a single executable named together (assuming they call each other properly;-) using the GCC suite of tools.

Microsoft's .NET is a popular way to use multiple languages together -- C#, F#, IronPython, IronRuby, and so on. Visual Studio will handle the compilations into compatible codes and the joining together in assemblies, but you can also do it "by hand" if you wish.

If by "compiling together" you mean having multiple different languages within the same file, that's also possible but rarer -- for example some C compilers have extensions to let you express "inline" assembly language within the same file.

Alex Martelli
Really.. can we create combined language/compiler ? Maybe in future it will be possible.. because all languages create a machine code which is independent of language .. what is stopping us from creating such thing ?
Xinus
Generally complexity of code, since you'd have to write basically two compilers in one plus some extra code to get it to work properly.
RCIX
A: 

It is really hard to cleanly handle Algol-68 thunks in C. Do-able, but I don't think I'd want to do it every day,

Richard Pennington
Algol60 is the one with thunks... Algol68 has array/string/matrix slicing... THAT would require a C-API to the Algol68 runtime. Other considerations would be nested functions, FORMAT types and threading. These could simply be side stepped for most code.
NevilleDNZ
+2  A: 

.Net platform is multi-lingual. Parrot is great for mixing Perl, Python, Ruby. What are you trying to do?

Hamish Grubijan
+1  A: 

Another good example of combining language is the Java platform. You can intermix Groovy, Jython, JRuby, Scala, Clojure, and other languages with Java. The different languages require different compilers, but you can generally call from one language to another. Groovy and Scala are particularly well suited for inter-operation.

Oh, and the Java Native Interface (JNI) lets you call C, C++, assembly and other languages from Java.

(The .NET platform shares these same attributes, as other posters have noted.)

Jim Ferrans
A: 

Take a look at Swig. It wrapes your C/C++ code so that you can call it from virtually any other language.

A: 

If you're using .net, you can compile your projects in different languages to netmodules and then link them into a single dll/exe. Visual Studio doesn't support this but msbuild does. The easiest way to get a compile is to edit a COPY of your .csproj file and change the output type to "module" and just run msbuild against it. Then use the "link" command to link your modules into your final exe/dll.

No Refunds No Returns