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:
Needs["Compile`"]
Create the C++ source file
code="extern \"C\"
double square(double a)
{return a * a;}
";
WriteString["/tmp/e.cpp",code]
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[{
LLVMString[llvmStr],
Function[{Typed[arg1, "Real64"]},
Native`PrimitiveFunction["square"][arg1]]
}];
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?
Program
andLLVMString
. Does one need someNeeds[]
? $\endgroup$ – Ruslan Jun 24 '19 at 14:01int square(int a)
$\endgroup$ – ilian Jun 28 '19 at 15:04