How can you pass values from one programming language to another?

Quora Feeds

Active Member
Garry Taylor


Lots of options, depending on your choice of languages.

Most languages can work with C. So say one language is C, and the other is Python, Java, C#, Lua, TCL, just about anything else, chances are there is a way to call C code from it. With Java, it’s generally JNI, with Python or maybe TCL it’s SWIG, plus the built in stuff for loading dynamic libraries.

Take Java, you write a Java class which declares some “native” methods, you then run a program called “javah” on that class file, that will give you a C header file. Then you implement the C functions using the definitions in the header file. Compile your C to a library, and Java can then load that up and start calling the C functions. It’s pretty cool.

If you don’t want to actually load up a dynamic library, you’ve got message passing, there are variants of this like MPI, or you can roll your own.

Then there is shared memory, basically you allocate some memory, mark it as shared, and you can have other processes access it. Doesn’t matter what language those processes are written in, so long as they can access shared memory regions.

Or of course, just run stuff. No reason you can’t call a Ruby script from Python, just run it, passing parameters, and reading the output.



See Questions On Quora

Continue reading...
 

joycemeyers

New Member
Lots of options, depending on your choice of languages.

Most languages can work with C. So say one language is C, and the other is Python, Java, C#, Lua, TCL, just about anything else, chances are there is a way to call C code from it. With Java, it’s generally JNI, with Python or maybe TCL it’s SWIG, plus the built in stuff for loading dynamic libraries.

Take Java, you write a Java class which declares some “native” methods, you then run a program called “javah” on that class file, that will give you a C header file. Then you implement the C functions using the definitions in the header file. Compile your C to a library, and Java can then load that up and start calling the C functions. It’s pretty cool.

If you don’t want to actually load up a dynamic library, you’ve got message passing, there are variants of this like MPI, or you can roll your own.

Then there is shared memory, basically you allocate some memory, mark it as shared, and you can have other processes access it. Doesn’t matter what language those processes are written in, so long as they can access shared memory regions.

Or of course, just run stuff. No reason you can’t call a Ruby script from Python, just run it, passing parameters, and reading the output.



See Questions On Quora rainbow friends

Continue reading...
I think there’s almost as many options as there are languages. Some languages have built-in ties to others, while some provide special means to call code from others, some compile to the same common language, and then there are several ways even separate programs can communicate with one another.

Specifically each of those have numerous implementations. E.g. Python does this in nearly all cases - calling code written in C. Even C itself tends to take object code written in assembly and combines it into the final program at compile time.
 
Last edited:
Top