Any tutorial for embedding Clang as script interpreter into C++ Code?
Asked Answered
P

5

17

I have no experience with llvm or clang, yet. From what I read clang is said to be easily embeddable Wikipedia-Clang, however, I did not find any tutorials about how to achieve this. So is it possible to provide the user of a c++ application with scripting-powers by JIT compiling and executing user-defined code at runtime? Would it be possible to call the applications own classes and methods and share objects?

edit: I'd prefer a C-like syntax for the script-languge (or even C++ itself)

Pellagra answered 11/7, 2010 at 20:16 Comment(0)
M
15

I don't know of any tutorial, but there is an example C interpreter in the Clang source that might be helpful. You can find it here: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/clang-interpreter/

You probably won't have much of a choice of syntax for your scripting language if you go this route. Clang only parses C, C++, and Objective C. If you want any variations, you may have your work cut out for you.

Mcmullan answered 12/7, 2010 at 14:2 Comment(4)
Thank you. It took me quite a while to get it up and running - basically because I didn't find the executable :) Now after looking into it I'd say it is half-way there. Now I need to figure out how to call functions that are defined in the non-jitted part. ... any ideas about that?Pellagra
I'm not sure what you mean. You should be able to get a pointer to any function that you've compiled. Maybe this general LLVM tutorial would be helpful: llvm.org/docs/tutorial (chapter 4, in particular). I don't expect that you'll be able to use this interpreter as it is, but it should give you an idea of how to embed a C/C++ interpreter in your application. (I haven't even used it myself; I just remembered seeing it.)Mcmullan
Ok here is what I did: lists.cs.uiuc.edu/pipermail/cfe-dev/2010-July/009836.html ... and it actually worked out with the help of the nice people from the cfe-dev mailing list. Why do you think this interpreter cannot be used in an app?Pellagra
I just figured you'd need to make some customizations or just general changes. If you don't have to, more power to you.Mcmullan
E
7

I think here's what exactly you described.

http://root.cern.ch/drupal/content/cling

Elbow answered 4/9, 2013 at 2:20 Comment(0)
B
6

You can use clang as a library to implement JIT compilation as stated by other answers. Then, you have to load up the compiled module (say, an .so library).

In order to accomplish this, you can use standard dlopen (unix) or LoadLibrary (windows) to load it, then use dlsym (unix) to dynamically reference compiled functions, say a "script" main()-like function whose name is known. Note that for C++ you would have to use mangled symbols. A portable alternative is e.g. GNU's libltdl.

As an alternative, the "script" may run automatically at load time by implementing module init functions or putting some static code (the constructor of a C++ globally defined object would be called immediately).

The loaded module can directly call anything in the main application. Of course symbols are known at compilation time by using the proper main app's header files.

If you want to easily add C++ "plugins" to your program, and know the component interface a priori (say your main application knows the name and interface of a loaded class from its .h before the module is loaded in memory), after you dynamically load the library the class is available to be used as if it was statically linked. Just be sure you do not try to instantiate a class' object before you dlopen() its module.

Using static code allows to implement nice automatic plugin registration mechanisms too.

Blastoff answered 18/5, 2013 at 9:59 Comment(0)
D
1

I don't know about Clang but you might want to look at Ch:

http://www.softintegration.com/

This is described as an embeddable or stand-alone c/c++ interpreter. There is a Dr. Dobbs article with examples of embedding it here:

http://www.drdobbs.com/architecture-and-design/212201774

I haven't done more than play with it but it seems to be a stable and mature product. It's commercial, closed-source, but the "standard" version is described as free for both personal and commercial use. However, looking at the license it seems that "commercial" may only include internal company use, not embedding in a product that is then sold or distributed. (I'm not a lawyer, so clearly one should check with SoftIntegration to be certain of the license terms.)

Dermatology answered 13/7, 2010 at 17:41 Comment(0)
L
0

I am not sure that embedding a C or C++ compiler like Clang is a good idea in your case. Because the "script", that is the (C or C++) code fed (at runtime!) can be arbitrary so be able to crash the entire application. You usually don't want faulty user input to be able to crash your application.

Be sure to read What every C programmer should know about undefined behavior because it is relevant and applies to C++ also (including any "C++ script" used by your application). Notice that, unfortunately, a lot of UB don't crash processes (for example a buffer overflow could corrupt some completely unrelated data).

If you want to embed an interpreter, choose something designed for that purpose, like Guile or Lua, and be careful that errors in the script don't crash the entire application. See this answer for a more detailed discussion of interpreter embedding.

Lordinwaiting answered 21/2, 2015 at 9:34 Comment(4)
If run inside a thread the thread will crash and not the main program.Wendall
@SteveMucci: this is OS specific, and usually false on Linux: a SIGSEGV would abort the entire processLordinwaiting
I just tested buffer overrun in Linux using pthreads. When put into main function the overrun crashed the entire program but when identical code was put into a thread it didn't.Wendall
@SteveMucci: That just means that you don't understand enough undefined behavior, and what every programmer should know about themLordinwaiting

© 2022 - 2024 — McMap. All rights reserved.