简体   繁体   中英

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

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/

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM