The Advanced Compilation Topics talk in the WTC 2018 shows that one can easily link C++ code through the new compiler technology. This feature seems very useful as one can easily load existing C++ code into Mathematica without extensive modifications.

I followed the instruction but unable to make it work:


Create the C++ source file

code="extern \"C\"
double square(double a)
{return a * a;}

Compile the C++ code to llvm

Run["clang -S -Wno-everything -O3 -o /tmp/e.ll -emit-llvm -march=skylake /tmp/e.cpp"]

Import the llvm

llvmStr = Import["/tmp/e.ll", "Text"];
prog = Program[{
    Function[{Typed[arg1, "Real64"]}, 

Compile to external program

fun = CompileToExternalFunction[prog]

I'm getting an error

Compile: CompilerException. Type kind is not handled

I'm using Mathematica V12 on ubuntu 16.04.

Any ideas about the error and the correct way to compile?

  • $\begingroup$ So basically an alternative to LibraryLink? Very interested! $\endgroup$ – Szabolcs Jun 24 '19 at 8:17
  • $\begingroup$ You ask ask a question, you don't explain a topic. $\endgroup$ – user64494 Jun 24 '19 at 11:24
  • $\begingroup$ Hm, I just get unevaluated Program and LLVMString. Does one need some Needs[]? $\endgroup$ – Ruslan Jun 24 '19 at 14:01
  • $\begingroup$ @Ruslan You need to load Compile. I've updated the post. $\endgroup$ – xslittlegrass Jun 24 '19 at 16:48
  • $\begingroup$ The code is correct and actually works in the current development version. The released version does not have the necessary handling for floating point types, but it would probably work with int square(int a) $\endgroup$ – ilian Jun 28 '19 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.