14

it is possible in C++ to execute the C++ code from string variable. Like in Javascript:

var theInstructions = "alert('Hello World'); var x = 100";

var F=new Function (theInstructions);

return(F());

I want something very similar like Javascript in C++. How to do that ?

8
  • no. it's not. you'd have to embed the entire C++ compiler complex within your app for such thing to even be remotely possible. Commented Apr 5, 2013 at 19:02
  • 4
    If you want an embedded scripting language, consider Lua. Commented Apr 5, 2013 at 19:04
  • @jrok: Of course it is. I actually did something like this in the past (for a kind of software shader implementation for an older ray tracer by me). The only "magic" is to invoke a compiler. Commented Apr 5, 2013 at 19:24
  • @phresnel i know it is, just not as easily and directly as OP hoped. Commented Apr 5, 2013 at 19:27
  • @jrok then why you come with first comment saying no, and then yes, it is not so constructive like that :( Commented Apr 5, 2013 at 19:29

6 Answers 6

12

No, C++ is a static typed, compiled to native binary language.

Although you could use LLVM JIT compilation, compile and link without interrupting the runtime. Should be doable, but it is just not in the domain of C++.

If you want a scripting engine under C++, you could use for example JS - it is by far the fastest dynamic solution out there. Lua, Python, Ruby are OK as well, but typically slower, which may not be a terrible thing considering you are just using it for scripting.

For example, in Qt you can do something like:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QScriptEngine engine;
    QScriptValue value = engine.evaluate("var a = 20; var b = 30; a + b");

    cout << value.toNumber();

    return a.exec();
}

And you will get 50 ;)

Sign up to request clarification or add additional context in comments.

2 Comments

You forgot about LuaJIT, which is close, if not there already, to being the fastest
@W.B. yes LuaJIT looks quite fast. Wish there was a head to head comparison to other languages, but the benchmarks on their site only compare to Lua.
11

You will need to invoke a compiler to compile the code. In addition, you will need to generate some code to wrap the string in a function declaration. Finally, you'll then somehow need to load the compiled code.

If I were doing this (which I would not) I would:

  1. Concatenate a standard wrapper function header around the code
  2. Invoke a compiler via the command line (system()) to build a shared library (.dll on windows or .so on linux)
  3. Load the shared library and map the function
  4. Invoke the function

This is really not the way you want to write C code in most cases.

3 Comments

This sounds like a plan, and I'm interested. Can you come with a windows simple example?
@Jigberto: You apparently haven't caught the underlying meaning of the answers here. There is nothing simple about this, because this is not what C++ is about. And if you ask for a simple example, you shouldn't be "interested" in this "plan".
@DevSolar - you know how curiosity works, he has to try it himself to see how pointless it is. Not entirely of course. I use a similar approach to compile and link QML components implemented in C++. But this is domain specific and works tightly coupled with JS.
6

Directly, no. But you can:

  • write that string to a file.
  • invoke the compiler and compile that file.
  • execute the resulting binary.

Comments

6

C++ is a compiled language. You compile C++ source into machine code, the executable. That is loaded and executed. The compiler knows about C++ (and has all the library headers available). The executable doesn't, and that is why it cannot turn a string into executable code. You can, indeed, execute the contents of a string if it happens to contain machine code instructions, but that is generally a very bad idea...

That doesn't mean that it wouldn't be possible to do this kind of run-time compilation. Very little (if, indeed, anything) is impossible in C++. But what you'd be doing would be implementing a C++ compiler object... look at other compiler projects before deciding you really want this.

Interpreted languages can do this with ease - they merely have to pass the string to the interpreter that is already running the program. They pay for this kind of flexibility in other regards.

Comments

1

You can use Cling as C++ interpreter.

I created small CMake project for easier Cling integration: C++ as compile-time scripting language (https://github.com/derofim/cling-cmake)

1 Comment

Can you include an example of correct usage? Answers that only include links to libraries are less useful than demonstrating how the library solves the problem.
0

Short answer is no. Hackers would have a field day. You can however use the Windows IActiveScriptSite interface to utilize Java/VB script. Google IActiveScriptSite, there are numerous examples on the web. Or you can do what I am currently doing, roll your own script engine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.