10

Is it possible to do these things from a native C or C++-compiled program (gcc):

  1. Call clang and compile given C code of a function, passed as const char *.
  2. Obtain a pointer and run it in the LLVM virtual machine.
  3. Acquire the result in the native program and continue.

How?

Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52
Cartesius00
  • 23,584
  • 43
  • 124
  • 195
  • I think a better more descriptive title is needed to catch attention of more SO users. – Nawaz Aug 11 '12 at 12:14
  • @Nawaz Good point, I've corrected it now. – Cartesius00 Aug 11 '12 at 12:15
  • 2
    Of course it is possible. See http://root.cern.ch/drupal/content/cling for example. – SK-logic Aug 11 '12 at 18:39
  • 3
    This is a duplicate of http://stackoverflow.com/questions/3224485/any-tutorial-for-embedding-clang-as-script-interpreter-into-c-code. – razeh Aug 12 '12 at 21:03
  • 1
    Have a look at Cling. For an example of a function in a C++ interpreter you can check this article: http://solarianprogrammer.com/2012/08/14/cling-cpp-11-interpreter/ – mmisu Aug 15 '12 at 12:09

1 Answers1

7

For a minimalistic example, see Clang interpreter.

Cling is bit more practical example of such an approach.

SK-logic
  • 9,605
  • 1
  • 23
  • 35
  • @James, it is an interactive C++ interpreter based on LLVM and Clang and optionally integrated into ROOT. Sort of a replacement for their older Cint thing. – SK-logic Aug 14 '12 at 06:51
  • This is what I posted below the code. Cling implements JIT which is offered by llvm, see this link on how to use it: http://llvm.org/docs/tutorial/LangImpl4.html – Robin Aug 14 '12 at 21:38