Any graceful way to jit some cpp code to recommand?

such as

int bar() {return 0;}
#include “foo.h”
A_GRACEFUL_WAY->call(“bar”) // → 0

I guess you want to compile some C++ code at runtime, and then run it. LLVM project does have building blocks for that (Clang, LLJIT, ORCv2), but I can’t find an official end-to-end example. Fortunately, I’ve put together a very similar project couple of years ago, save for the fact that I’ve also generated sources at runtime. At high level steps are the following:

  1. Create InMemoryFileSystem, and load your sources there (could be skipped if you have sources on the disk) (line 317)
  2. Create CompilerInvocation that holds options to pass to Clang (line 320)
  3. Execute EmitLLVMOnlyAction to get LLVM IR out of your C++ sources (line 330)
  4. Move the output of that action into ThreadSafeModule object (line 363)
  5. Add that module to LLJIT object (line 365)
  6. Search for the symbol you need (line 369)
  7. Get its address and cast it to the correct pointer type (line 371)

Function pointer that you’ve got is fully functional.
Things to be beware of are name mangling, templates (that are not going to magically instantiate themselves), and standard library (out of the box you don’t get either headers nor symbols to link against, including ::operator new). In my example linking is done against the very executable that compiles the code.

My project was written against LLVM 10, but I don’t expect APIs to change drastically since then. Another useful reference is Cling. There you can see them exporting symbols from cling executable to address some of the issues I mentioned earlier.

Thank you for your very detailed explanation. I will spend time look into it.