3

Can I write a program in two different languages and compile them together in one LLVM executable?

For example, part of my program is in C++, and part of it is in D.

1
  • the compiler is allowed to say yes, maybe but the ABI will surely say no no no Commented Jun 5, 2014 at 21:34

1 Answer 1

5

Not in the general case. Only if the languages are ABI-compatible. This is true for C and C++ to a very limited degree (extern "C" code from the C++ side), and much less so for other languages.

Sign up to request clarification or add additional context in comments.

4 Comments

Shouldn't such compatibility be much easier if both languages are compiled with LLVM?
@VictorLyuboslavsky: if you invest extra work into it. Both Julia and Haskell (via GHC) compile to LLVM IR. It doesn't mean you can just call any Haskell function from Julia
@EliBendersky If they both compile to LLVM-IR, can the two IRs be combined using llvm-link and then be interpreted with lli successfully?
@mishr: no, it's not that simple. LLVM IR cannot represent any non-trivial data structures - this is the ABI, and the frontend handles this. So if you pass some object from one language to another, LLVM IR cannot magically know how to decompose the object in one language and rebuild it in another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.