Neros
Neros

Reputation: 1129

c++ python 3 binding

I am trying to bind python3 in C++.

When using this:

Py_SetProgramName(argv[0]);

it gives this error:

error C2664: 'Py_SetProgramName' : cannot convert parameter 1 from 'char *' to 'wchar_t *'

Even though that's how the documentation example shows to do it.

I also tried this:

Py_SetProgramName((wchar_t*)argv[0]);

But apparently that's the wrong way to do it.

So how do I fix this, and is there any other good resources on binding Python 3 in C++?

Upvotes: 7

Views: 2621

Answers (3)

Pmp P.
Pmp P.

Reputation: 507

The official way of converting from char to wchar_t is now :

wchar_t *program = Py_DecodeLocale(argv[0], NULL);
Py_SetProgramName(program);

on a side note mbstowcs is not reliable on some platforms.

A quite good example of using python2/3 with c++ would be Panda3D. a c++ game engine scripted with python, that also provides a c++ module builder.

Upvotes: 4

falsetru
falsetru

Reputation: 368894

Try following:

wchar_t progname[FILENAME_MAX + 1];
mbstowcs(progname, argv[0], strlen(argv[0]) + 1);
Py_SetProgramName(progname);

http://www.cplusplus.com/reference/cstdlib/mbstowcs/

Upvotes: 3

cdarke
cdarke

Reputation: 44344

I suggest you look at this question

The example documentation for the Python 3 API appears to have not been upgraded from Python 2 - the example you show is one of them (I have reported some of the others).

I have found no good documentation in this area. Even the new (Python 3) editions of well-known Python books either cover this subject sparsely or have code errors (usually because the code comes from Py2).

Upvotes: 1

Related Questions