Including scripting support in C++ application

Hi! What scripting language would You recommend from the set of Python, Common Lisp, Scheme to be used in a C++ application? The main aspect to consider should be the easiness of embedding its support in the application.
Last edited on
Embedding a scripting language in C++?

Can you explain what effect you're trying to achieve?
C++ - GUI and core. Scripting language - easily addable modules.
Python, Lua or Javascript. I'd go for Python myself.
Lua and JavaScript are not options for me. I like Python and Lisp so I want to choose between them. The problem is that I am not sure which would be easier to embed.
Well, I happen to have here a short program I wrote some time ago to test Python embedding:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#undef _DEBUG
#include <python.h>
#include <iostream>

int main(){
	Py_Initialize();
	//FILE *file=fopen("hello.py","r");
	//PyRun_SimpleFile(file,"hello.py");
	//std::cout <<PyRun_SimpleString("print \"Hello, World!\"\nwait")<<std::endl;
	PyObject *str=PyByteArray_FromStringAndSize("Hello,\0W\norld!\n",15),
		*filename=PyString_FromString("hello"),
		*module=PyImport_Import(filename);
	Py_DECREF(filename);
	if (!module){
		Py_DECREF(str);
		return 1;
	}
	PyObject *function=PyObject_GetAttrString(module,"f"),
		*function2=PyObject_GetAttrString(module,"f2"),
		*args=PyTuple_New(1);
	if (function && PyCallable_Check(function)){
		PyTuple_SetItem(args,0,str);
		PyObject_CallObject(function,args);
		Py_DECREF(args);
		Py_DECREF(function);
		Py_DECREF(str);
	}
	if (function && PyCallable_Check(function2)){
		for (char a=0;a<10;a++){
			PyObject *return_value=PyObject_CallObject(function2,0);
			if (return_value){
				printf("f() returned %d.\n",PyInt_AsLong(return_value));
				Py_DECREF(return_value);
			}
		}
		Py_DECREF(function2);
	}
	Py_DECREF(module);
	Py_Finalize();
	//fclose(file);
	system("pause"); //Yeah, I know. I was being lazy.
	return 0;
}
//hello.py (FIOC makes me want to throw up):
#print "Hello, World!"

def f(a):
	for x in a:
		print x

call=0

def fibonacci(n):
	if n<2:
		return 1
	a=1
	b=1
	c=0
	for d in range(2,n+1):
		c=a+b
		a=b
		b=c
	return c

def f2():
	global call
	a=fibonacci(call)
	call+=1
	return a
Last edited on
I am unable to find right compiler/linker options.
You need to install Python, then you need to make sure the system can load the libraries, then make sure the compiler can see the headers, then you need to link the static libraries to the other modules.
I am using Ubuntu 9.04, have installed libpython3-dev and GCC can see headers. Just don't know the linker options.
I can't seem to find the header that declares PyByteArray_FromStringAndSize(). I may have an old version installed. Try compiling with -lpython2.5 or, given your version, -lpython3
You can take a look at the libraries you have available by ls *python*ing in /usr/lib, or perhaps /usr/local/lib.
Last edited on
Got it working. Thanks.
Topic archived. No new replies allowed.