简体   繁体   English

c ++ python 3绑定

[英]c++ python 3 binding

I am trying to bind python3 in C++. 我试图在C ++中绑定python3。

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++? 那么我该如何解决这个问题呢,在C ++中绑定Python 3还有其他好的资源吗?

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/ http://www.cplusplus.com/reference/cstdlib/mbstowcs/

The official way of converting from char to wchar_t is now : 从char转换为wchar_t的官方方法现在是:

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

on a side note mbstowcs is not reliable on some platforms. 一方面,mbstowcs在某些平台上不可靠。

A quite good example of using python2/3 with c++ would be Panda3D. 使用python2 / 3和c ++的一个很好的例子是Panda3D。 a c++ game engine scripted with python, that also provides a c++ module builder. 一个用python编写的c ++游戏引擎,它还提供了一个c ++模块构建器。

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). Python 3 API的示例文档似乎尚未从Python 2升级 - 您展示的示例就是其中之一(我已经报告了其他一些)。

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). 即使是着名的Python书籍的新(Python 3)版本也要么稀疏地覆盖这个主题,要么有代码错误(通常因为代码来自Py2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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