How do multiple languages interact in one project?
Asked Answered
C

13

102

I heard some people program in multiple languages in one project. I can't imagine how the languages interact with each other.

I mean there is no Java method like

myProgram.callCfunction(parameters);

never happens or am I wrong?

Conglomeration answered 11/3, 2009 at 23:57 Comment(0)
C
45

Having multiple languages in one project is actually quite common, however the principles behind are not always simple.

In the simple case, different languages are compiled to the same code. For example, C and C++ code typically is compiled into machine assembler or C# and VB.Net is compiled into IL (the language understood by the .NET runtime).

It gets more difficult if the languages/compilers use a differnt type system. There can be many different ways, basic data types such as integer, float and doubles are represented internally, and there is even more ways to represent strings. When passing types around between the different languages it must be sure that both sides interpret the type the same or - if not - the types are correctly mapped. This sort of type mapping is also known as marshalling.

Classic examples of interoperability between different program languages are (mostly from the Windows world):

  • The various languages available for the .NET platfrom. This includes C#, VB.Net, J#, IronRuby, F#, XSLT and many other less popular languages.
  • Native COM components written in C++ or VB can be used with a huge variety of languages: VBScript, VB, all .NET languages, Java
  • Win32 api functions can be called from .NET or VB
  • IPC (inter process communication)
  • Corba, probably the most comprehensive (and most complex) approach
  • Web services and other service-oriented architectures, probably the most modern approach
Charlean answered 12/3, 2009 at 0:7 Comment(5)
May I ask what Native COM components are?Pronounced
Is it a good practice to call python scripts from c# code in large project?Ombudsman
@amrit: This totally depends. Of course, mixing technologies in a project always comes with a certain cost, but this cost may easily be outweighed by the cost of (re-)implementing existing functionality. So it may be perfectly reasonable to integrate existing Python libraries into a C# codebase.Charlean
The link should be to en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture, not to en.wikipedia.org/wiki/Corba (though the corba in the second link does look delicious).Benton
@Jasha: haha... thanks for spotting this!Charlean
S
23

Generally, any decently sized web project will use about five languages: HTML, CSS, Javascript, some kind of server-side “getting things done” language (ASP, JSP, CGI scripts with Perl, PHP, etc.), and some variant of SQL for database connectivity.

(This is, of course, hand-waving away the argument about whether or not HTML and CSS count as programming languages – I’m the “they are, but just not Turing-complete languages” camp, but that’s a whole other thread.)

Some examples of how all those work together:

If you’re going the best-practices route, the structure of a web page is in HTML, and the instructions for how to display it are in CSS – which could be in the same file, but don’t have to be. The CSS contains a bunch of classes, which the HTML refers to, and it’s up to the browser to figure out how to click them together.

Taking all that a step further, any javascript scripts on that page can alter any of the HTML/CSS that is present (change contents of HTML entities, swap out one CSS class for another, change the behavior of the CSS, and so on.) It does this via something called the Document Object Model, which is essentially a language and platform-independent API to manipulate HTML pages in an object-like manner (at which point I’ll back away slowly and just provide a link to the relevant wiki article.)

But then, where does all the HTML / CSS / Javascript come from? That’s what the server-side language does. In the simplest form, the serer-side language is a program that returns a giant string holding an HTML page as its output. This, obviously, can get much more complex: HTML forms and query string parameters can be used as input for our server side program, and then you have the whole AJAX thing where the javascript gets to send data directly to the server language as well. You can also get fancy where the server language can customize the HTML, CSS, and Javascript that gets spit out – essentially, you have a program in one language writing a program in another language.

The Server-side language to SQL connection works much the same. There are a lot of ways to make it both more complex and safer, but the simplest way is for your server language to dynamically build a string with a SQL command in it, hand that to the database via some kind of connector, and get back a result set. (This is a case where you really do have a function that boils down to someValue = database.executeThisSQLCommand( SQLString ). )

So to wrap this up, different languages in this case either communicate by actually writing programs in each other, or by handing data around in very simple easy to parse formats that everybody can understand. (Strings, mainly.)

Shaikh answered 12/3, 2009 at 1:50 Comment(1)
Unless you're using Seaside on Gemstone (GLASS) Smalltalk. It achieves its order of magnitude better productivity by doing everything in the server-side language.Bufford
D
16

Multiple languages in use is called "interoperability" or "interop" for short.

Your example is wrong. Java can call C functions.

The language provides a mechanism for interoperability.

In the case of .NET, languages are compiled into IL as part of the CLI. Thus any .NET language can interop (call methods defined by) modules defined in any other .NET language.

As an example:

I can define a method in C#

static void Hello(){ Console.WriteLine("Hello World");}

And I can call it from Python (IronPython)

 Hello()

And get the expected output.

Generally speaking, some languages interop better than others, especially if the language authors specifically made interop a feature of the language.

Dar answered 12/3, 2009 at 0:3 Comment(0)
A
16

Multiple languages can interact with:

  1. Piped input/output (ANY language can do this because input and output must by necessity be implemented in every non-toy language)
  2. Having code in one language compile to a native library while the other supports calling native code.
  3. Communicating over a loopback network connection. You can run into difficulties with firewall interference this way.
  4. Databases. These can be thought of as a "universal" data storage format, and hence can be accessed by most languages with database extensions. This generally requires one program to finish operation before the next program can access the database. In addition, all 'communications' are generally written to disk.
  5. If the languages involved run on the same runtime (i.e. .NET, JVM), then you generally can pass object data from one language directly to the other with little impedence.

In almost every case, you have to convert any communication to a common format before it can be exchanged (the exception is languages on the same runtime). This is why multiple languages are rarely used in one project.

Aircool answered 12/3, 2009 at 4:51 Comment(0)
F
5

I work on a large enterprise project which is comprised of (at the last count) about 8 languages. The majority of the communication is via an enterprise level message bus which contains bindings for multiple languages to tap into and pass data back and forth. It's called tibco.

Favus answered 12/3, 2009 at 1:51 Comment(0)
C
4

There are many different ways you can use different languages in one project There are two main categories that come to mind:

  1. Using Different languages together to build one application. For example using Java to build the GUI and using JNI to access C API (so answering your question you can call C functions from Java ;))

  2. Using different languages together in the one project if they are not part of the same application. For example. I am currently working on an iPhone app that has uses a large amount of text. I am currently using three languages: Python (to work with the original sources of the text), SQL (to put the results of the python app in a format easily accessible from the iPhone sqlite3 API) and Objective C to build the actual app. Even though the final product will only be Objective C, I've used two other languages to get to the final product.

Clarisaclarise answered 12/3, 2009 at 0:8 Comment(0)
L
3

You could have an app where the bulk of the work is done in Java, but there might be some portion of it, like maybe a data parser or something is written in Python or what have you. Almost two separate apps really, perhaps the parser is just doing some work on files and then your main app in Java is using them for something. If someone were to ask me what I used in this project I'd say "Java and Python."

Lapse answered 12/3, 2009 at 0:6 Comment(2)
ok.granted. this is a way .. how languages can interact. BUT : When does the Python program know, that there is something to parse... and when does the main app know, that the parser has just finished ?Conglomeration
Ok, here's an example. A company I worked for had a parser app out on a network that listened for incoming requests to parse data. The parser just listened for requests from another app and then parsed and shoved the data along. The parser was written in C, the app in C#.Lapse
H
3

There are various ways that multiple languages can be used in one project. Some examples:

  • You can write a DLL in, say, C, and then use that library from, say, a VB program.
  • You could write a server program in, say C++, and have lots of different language implementations of the client.
  • A web project often uses lots of languages; for example a server program, written in, say, Java (a programming language), which fetches data from a database using SQL (a query language), sends the result to the browser in HTML (a markup language), which the user can interact with using Javascript (a scripting language)...
Hydra answered 12/3, 2009 at 0:13 Comment(0)
B
3

Badly. If there is no urgent need, stick to a single language. You're increasing dependencies and complexity. But when you have existing code providing interesting functionality, it can be easier to glue it together than to recreate it.

Bufford answered 12/3, 2009 at 0:14 Comment(0)
S
2

It depends on the type of project. If you want to experiment, you can set up a web project, in .NET, and change the language on a page by page basis. It does not work as you show in your pseudocode, but it is multiple languages. Of course, the actual code directory must be a single language.

Secunderabad answered 12/3, 2009 at 0:2 Comment(0)
N
2

There are a couple of ways in which code in languages can interact directly. As long as the data being passed between the code is in the right format, at the bits and bytes level, then there is no reason why different languages can't interop. This approach is used in traditional windows DLL development. Even on different platforms, if you can get the format correct (look at big/little endian if interested) it will work as long as your linker (not compiler) knows how to join code together.

Beyond that there are numerous other ways in which languages can talk to each other. In the .Net world code is compiled down to IL code, which is the same for every language, in this way C#, VB.Net are all the same under the hood and can call/work with each other seamlessly.

Nimrod answered 12/3, 2009 at 0:9 Comment(0)
C
2

Just to add to the list of examples, it's fairly common to optimize Python code in C or C++ or write a C library to bind another library to Python.

Cypsela answered 12/3, 2009 at 1:7 Comment(0)
L
0

the inter process communication is a better way for do this if you want using nodejs with python for call a python function in nodejs, look on this code

index.js

let { spawn } = require("child_process");

// spawn python file as child process
// replace "py" with "python" on linux
let PythonSpawn = spawn("py", [join(__dirname, "index.py"), "say_hello","MaDz"]);
PythonSpawn.stdout.on("data", (data) => {
console.log(data.toString());
});

module.py

def say_hello(name):
print(f"hello {name}")

index.py

import sys
from module import say_hello
if sys.argv[1] == "say_hello":
say_hello(sys.argv[2])

if you run index.js file, that will spawn child process using py app and pass the index.py file path as argv for run the file, the index.py will check the argv and if the argv in index 1 is say_hello, will use the function say_hello from the module and pass the name exists in argv[2] to the function, finally, the print function will write in stdout and the javascript file is listening for event "data" coming from the stdout, then the index.js after get the data, will print in the terminal using console.log function, the output is "hello MaDz"

in advanced ways, the apps use stdin with stdout and does not depending on argv, i'm not profissional in python for do this

Lamellar answered 1/5, 2023 at 23:3 Comment(1)
Please read [How do I format my posts using Markdown or HTML? ](stackoverflow.com/help/formatting)Ravens

© 2022 - 2024 — McMap. All rights reserved.