-1

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?

closed as unclear what you're asking by whatsisname, gnat, Jörg W Mittag, Nicol Bolas, TZHX Sep 15 '16 at 8:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Is there any way to not mix c and c++? I'm not sure c++ would even work without the c in it. Same for java and c# come to think of it. Lisp. Lisp is c free. Isn't it? Hmm.... Oh wait assignments are in c. Dang um. brainfuck? – candied_orange Sep 14 '16 at 4:45
  • i think that this question is pointless unless you explain what exactly do you mean by mixing languages – nicks Sep 15 '16 at 11:05
  • Mixing languages happens a lot. The "controlling language" needs to have features to support the "other language". A lot of languages have support for inline assembler code. It's not c and c++ but an example of how a host language supports another language. – Pieter B Sep 15 '16 at 11:12
  • If you start looking into shared libraries, mixing languages is almost inevitably happening if you program in something else but C. – tofro Sep 19 '16 at 8:46
13

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.

8

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.

0

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,%eax\n\t"
       "movl $0xff,%ebx\n\t"
       "int $0x80\n\t"
        ...
     );
  // 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.

  • 2
    there are plenty of cases of langauges that allow other languages to be used inline e.g. inline assembly. so the first sentence is blatantly untrue. – jk. Sep 15 '16 at 6:05
  • 1
    Or Inline::C ... – reinierpost Sep 15 '16 at 8:45
  • @jk - see my edit. – John Bode Sep 15 '16 at 15:04
-1

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 😬

  • Please comment when you vote down ;) – yeoman Sep 15 '16 at 9:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.