8

I'm really new to C++ and I've come across a problem I've not been able to solve by reading documentations.

I want to embed a script language into my c++ application. That language could be javascript, lua or preferably python.

I'm not looking for something like Boost.Python / swig, something that is able to wrap my c++ functions / classes to a python interface, but rather a python_evaluate_and_return_result_as_variable("my_code"); function.

I have a whole bunch of structs containing a few integers:

struct my_integers {
    int a;
    int b;
    int c;
    int d;
    int e;
};

Now I want to do some math with these integers, for example:

i.a = i.c * i.e;

The math I want to do will be changing a lot in the future and I need people other then me be able to change the math without having access to the c++ code.

I'm thinking about a code structure like this:

  1. I initialize my struct and fill it with the starting values
  2. I load an external python function, lets say "my_python_function", that takes the struct as an argument and does so math with it before returning it.
  3. I get my struct like i = my_python_function_cppwrapper(i)

Is something like that possible? I googled a lot for this but the only thing I seem to find are wrappers that provide c++ -> python (or the other way around) functionallity without really interacting with variables.

I'd be really thankful for any help,
Robin.

Robin
  • 1,322
  • 1
  • 13
  • 23

9 Answers9

8

The Python documentation has a page on embedding Python in a C or C++ application.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
8

Why not use Boost.Python? You can expose your data classes to Python and execute a script/function as described here.

David Feurle
  • 2,687
  • 22
  • 38
  • Thank you very much, that was what I've been looking for. I must've missed the section when I looked over Boost.Python. – Robin Sep 24 '10 at 07:27
6

If you want to simply run Python scripts from C/C++, then use the Python C API. In your C/C++ code:

PyRun_SimpleString("import math; x = math.sqrt(2 * 2)");

For more complicated things, you will have to look at the API docs, but it's pretty straightforward.

Ross Light
  • 4,769
  • 1
  • 26
  • 37
  • That doesn't quite work for me as I need to pass and extract variables, but thanks for your comment :) – Robin Sep 24 '10 at 07:27
2

How about embedding a JavaScript engine, such as V8?

Matthijs Bierman
  • 1,720
  • 9
  • 14
  • I think I'm sticking with python but I may use V8 in my next project. Thanks for the hint, the only javascript engine I could think about was spidermonkey. – Robin Sep 24 '10 at 07:28
1

dont forget the grand-daddy of embedded scripting language - tcl.

tcl has v nice c++ wrapper (modelled on boost.python) that makes it trivial to invoke and to wire up callbacks to your code

pm100
  • 48,078
  • 23
  • 82
  • 145
1

Lua works pretty well too, especially since its small, is ansi c compliant, has a low memory foot print along with a great wiki and messaging list. If you need even more speed there is a x86 32 and 64 bit jit version(luajit). Binding can be done with an array of tools/libraries, like swig or lunar(the wiki lists them all). The only problem that i can see is binding the struct members so they can be referenced directly(ie: struct.member = 4), though its possible to set this up with metatables that have get and set methods bound to variable names

Necrolis
  • 25,836
  • 3
  • 63
  • 101
1

You say that you're not looking for something to wrap your C++ functions / classes in a Python interface, but if you want Python code to be able to refer to members of your C++ my_integers structure, that is wrapping C++ classes in a Python interface. Of course, you're free to wrap as many or as few classes as you want - in this example, you'd wrap my_integers, then you'd embed a Python interpreter to do stuff with my_integers.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
1

For something as simple as you describe, you could implement an interpreter for your own 'little language'. You could even call it the "Robin" language. ;-)

martineau
  • 119,623
  • 25
  • 170
  • 301
1

I advice using Lua as internal scripting engine. Implementation is just a few lines, and though light, the language has sufficient power. So no need for TCL. You might as well look at python, integration in C++ is rather easy, as there exists a Boost.Python implementation facilitating integration.

But depending on the application, I'd still recommend Lua.

polemon
  • 4,722
  • 3
  • 37
  • 48