Is there any way to mix languages? [closed]

I am thinking how programmers mix languages.

If I want to mix C and C++ language then what should I do?

And how to mix languages?

4

Well, firstly, C is more or less a subset of C++, so there is some subset of the two languages that will easily compile in both language compilers.

Beyond that, languages talk to each other mostly through Application Program Interfaces (API’s) and foreign-function interfaces (FFI’s).

An example of two languages talking through an API is a JSON object sent to a web service from Javascript in a web browser to a micro-service written in Java.

An example of two languages talking through an FFI is Python talking to a C library. Many languages have a C FFI.

0

I am posting additional answer to add more useful details not present in already accepted (and good) Robert Harvey’s answer.

Many languages have multiple ways to communicate with another languages, everything depends on relationship between the two codebases. Some of the existing possibilities:

Languages based on the same environment – just like you can easily call C functions from C++, environments like Java Virtual Machine (Java, Scala) or Common Language Runtime (C#, VB.NET) allow you to easily communicate with code written in any language compiling to the same enviroment. All libraries should be compatible, so you can easily use the same codebase in order to make ‘talking to another program’ easier. It is also possible to simply include program in different language as a library to the project in another language. Integration is mostly seamless.

Scripting engines and languages – many languages have libraries which allow you to integrate scripting engines into your code. Most often, you can choose objects and methods that will be visible for the scripting engine, allowing it to communicate with your ‘main’ program and use the same libraries. The main advantage of this approach is that you can have parts of code that can be changed on a fly without a need to redeploy or even restart the application, but – if you want to expose them outside of your application – you are risking high security risk. It requires some work as well.

For talking to an API and bridge between two different environments approaches, please refer to Robert Harvey’s accepted answer.

Obviously, you cannot mix multiple languages in the same source file (unless you writing an IOCCC entry).

As jk and reinierpost point out in the comments below, there are are ways to mix different languages in the same source file, but they either require special syntax, keywords, or preprocessing steps to do so. For example, gcc offers an extension that allows you to mix assembler and C in the same source file using a special asm keyword:

void foo( void )
{
  // some C code
  asm( "movl %ecx,%eaxnt"
       "movl $0xff,%ebxnt"
       "int $0x80nt"
        ...
     );
  // some more C code
} 

What you cannot do is write

void foo( void )
{
  // some C code
  movl %ecx, %eax
  movl $0xff, %ebx
  int $0x80
  ...
  // more C code
}

and compile it as C code. Absent the asm keyword, the C compiler will choke on the first line of assembler.

I’ve also used SQL directly embedded within C code – in those cases, you had to use special keywords (EXEC SQL) and you had to run the source file through a preprocessor, which would convert the SQL to valid C code. Then you’d pass the preprocessed source code to the C compiler.

Again, what you cannot do is simply mix SQL and C code in the same source file and compile it as C (or execute it as SQL).

For situations where inline language mixing isn’t supported, you can write separate source files in different languages, compile each into separate object files, and then link the object files together in a single program. How easy this is to do depends on the languages involved and their calling conventions.

It’s fairly easy to combine code written in C and C++, since they follow many of the same conventions and use the same type representations. It’s somewhat less easy to combine code written in C and Fortran1.

Then there’s combinations of Java code running in a VM calling native C or C++ code. Again, there’s an interoperability layer (the Java Native Interface) that’s required.


  1. All my first-hand experience is with Fortran 77, which has been superceded a couple of times by now, but I think these issues still apply with modern Fortran:
    • Fortran and C treat string data very differently; Fortran stores metadata about the string length with the string, whereas C does not.
    • Fortran lays out elements in multidimensional arrays in column-major order, C lays them out in row-major order.

3

Mixing languages comes in several varieties, basically in process, out of process on the same machine, and out of process across the network.

The performance cost of in process mixing depends on whether marshalling complex arguments is necessary, or not.

Marshalling is basically the act of copying the content of a data structure into a data structure that is understood by the side into which a function calls.

In the in-process C / C++ case, it’s free, just as in the C / Objective-C case, as all of these languages can directly handle C data structures.

In the cases of in-process C / Java and C / C#, things start getting more complicated, as only some specific types can be used across the border without marshalling.

In the out of process cases, everything needs to be marshalled, which is way slower even across super fast networks than locally in memory.

The in-process C / C++ case you seem to be most interested in is in fact the foundation of both Windows and the macOS kernel. In both cases, very low-level things are implemented in C, while higher-level code in C++ directly uses the C APIs that the lower level code provides, with no performance penalty at all.

Just keep in mind that C++ is NOT a strict superset of C, mostly because of keywords. As long as you don’t name your functions “namespace”, “template”, or “auto”, you should be fine, though ?

1

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *