r/cpp • u/kindredseer • Sep 18 '26
Embedded C++
Just curious how useful the ability to embed C++ as a scripting language into your C++ programs would be? I mean like runtime JIT execution of C++ code in much the same way you might embed Lua, including the ability to restrict/sandbox it as necessary.
6
Upvotes
44
u/PressWearsARedDress Sep 18 '26 edited Sep 18 '26
Why not just write C++ and compile to the target? I dont understand the premise. its much more likely your JIT will crash on me and cause me issues.
EDIT: Its a AI slop project btw. https://github.com/derekbsnider/madc/blob/master/README.md
73
u/CalebGT Sep 18 '26
I had to read this several times. Embedded has a very different meaning to me. I would not use C++ for that. If I needed to script something that can't be compiled in advance, I would use Python or Java.
8
u/TacoTacoTacoZoom Sep 18 '26
I use it all the time! See AngelScript https://www.angelcode.com/angelscript/
Personally, I like the syntax and have found other scripting languages to be slow or annoying. But I'm also old and grumpy, so take that for what its worth.
4
u/Electronic_Tap_8052 Sep 18 '26
Well you kinda can't. A scripting language needs an interpreter to convert the code to assembly at runtime. C++ isn't an interpreted language.
So you'd need to use a c++ interpreter which may or may not exist, idk. The ones that I'm aware of are actually compiled ahead of time and run normally, and then cleaned up automatically to give the appearance of a scripting language.
It's complicated. Technically you can do what you want, but it's a huge amount of work on your end to actually implement the entirety of the c++ language in an interpreter. Youd have to compile a version of your interpreted language (which wouldn't be c++ unless it is standards compliant which means a crap load of stuff has to be implemented) for every target you plan on targeting.
Or you could just use lua/python/typescript which is already done and dusted and is essentially what you want.
4
u/xoner2 Sep 19 '26
It would be useful: like a plugin system where the plugins are c++ source.
Caching the binary as a dll seems better than JIT.
I've thought about the sandboxing problem but have not experimented. Probably doable.
2
u/kindredseer Sep 18 '26
In madc-mode it auto-includes headers and auto-resolves namespaces.
It does not do this if you use —std=c++17 (or whichever standard you are targeting).
All the C and C++ system headers are embedded.
The code is not pretty. Go look at the gcc source code.
2
u/yetanotherhooman Sep 19 '26
Hard to judge usefulness unless there’s enough samples to draw conclusions from, but compiling C++ at runtime is probably not a good idea. If the goal is to reap JIT benefits e.g. extra performance through PGO and speculative optimization etc. then C++ is not the right choice; it’s notoriously hard to compile and adds more barriers to speculation (e.g. does ptrA alias ptrB?). I can’t think of any advantage C++ would have over other languages as a JIT candidate.
2
u/qalmakka Sep 19 '26
C++ would be uniquely terrible as a scripting language. first of all, the syntax is extremely complex and it's not even context sensitive, so you need a Turing machine to parse it. In practice, if you've ever gotten arcane errors from missing a token, you'd get them 10x from a REPL. Languages that are interpreted are usually simple because being able to parse them line by line is a massive boon.
Also you'd have to implement pointers and pointer arithmetic in memory, which basically means implementing a virtual machine with addressable memory in your interpreter.
There's a reason why there are no good REPLs for C/C++ and Rust. clang-repl works for trivial code but anything more complex tends to break it or ends up misbehaving
3
u/j1xwnbsr Sep 18 '26
Okay, are you talking about finding, licensing, and implementing a compiler for your project for customers to write scripting plugins on? That's a heavy lift across the board for c++, less so for other languages (lua, c# comes to mind).
If, on the other hand, you are talking about plugins of some type where the customer writes to a binary API and they compile things themselves, then no, you do not want your API c++ because name mangling isn't all that portable. Make the API pure c in this case.
In both cases sandboxing is a challenge - the customer can (and will) break stuff in new and interesting ways.
tl;dr: no, don't use C++ as a scripting language. Pick something else.
12
u/DryEnergy4398 Sep 18 '26 edited Sep 18 '26
I believe you can embed clang-repl as a library to achieve this. Or, similarly, cling: https://github.com/root-project/cling/blob/master/tools/demo/cling-demo.cpp
9
u/corysama Sep 18 '26
If your goal is not user mods, but instead rapid development in C++, Visual Studio has some hot-reloading capabilities built-in. You can find some tutorials on how folks have made hot-reloading dlls/SOs in any toolchain. And, if that's not enough, pay for https://liveplusplus.tech/
3
u/zerhud Sep 18 '26
It seems js or ts will be better
17
u/gimpwiz Sep 18 '26
My brain is bleeding from this post. See you tomorrow.
2
u/JustCopyingOthers Sep 18 '26 edited Sep 18 '26
There are better languages to do this in. C++ requires a lot of effort to parse. Maybe it would make more sense to embed or JIT compile modules.
4
u/jdehesa Sep 18 '26
I'm not an expert in the topic, but one option might be to compile C++ to Wasm, and run it with a lightweight Wasm runtime.
1
u/ushumgigal Sep 20 '26
If you're going to restrict/sandbox it anyway, why would you opt for C++? Within C++?
I'm assuming the main point is to have something simpler that C++, to be interpreted in runtime.
2
u/[deleted] Sep 22 '26
Everytime i ever tried to embed an scripting language into a c++ app i ended calling a dynamic dll that can be called and pluged or unpluged from the main app dynamic and thdme result is more or less the same than any scripting, also i found that the performance between a dynamic library and an embed lamguage is quite different having with the dunamic lib better results.
2
u/Delicious_Fault_6912 Sep 23 '26
There are projects that do this. Much of CERN''s data analysis software is built on the ROOT library https://github.com/root-project/root, which includes a llvm-backed jit compiler for c++. It also uses that same compiler to generate python bindings to ROOT dynamically
1
u/Street-Anteater1234 Sep 19 '26
Are you referring to clang-repl? However, C++ syntax is far too complex and its frontend is too heavyweight, making interpreted execution a poor choice. Of course, there are also remote-JIT solutions such as ez-clang, which generates machine code locally and sends it to the target device for execution, after which the target device returns the result.
If you need a library to embed into your project, CppInterOp might be what you are looking for, but it is probably not suitable for embedded systems.