Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute C++ from String variable

Tags:

c++

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 ?

like image 435
Jigberto Avatar asked Apr 05 '13 19:04

Jigberto


People also ask

Can we use string variable in C?

C does not directly support string as a data type, as seen in other programming languages like C++. Hence, character arrays must be used to display a string in C. The general syntax of declaring a string in C is as follows: char variable[array_size];

How do I Scanf a string?

We can take string input in C using scanf(“%s”, str). But, it accepts string only until it finds the first space. There are 4 methods by which the C program accepts a string with space in the form of user input.

What Is syntax of string in C?

Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.

How do you run a string in C++?

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++.


2 Answers

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 ;)

like image 69
dtech Avatar answered Oct 01 '22 21:10

dtech


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.

like image 38
tletnes Avatar answered Oct 01 '22 20:10

tletnes