Swig无法将python3的字节对象转换为std::string

Swig unable to convert byte objectof python3 to std::string

本文关键字:转换 std 对象 string 字节 python3 Swig      更新时间:2023-10-16

我正在尝试使用SWIG将python3与C++接口,它不断抛出以下错误

>> ipc.sendMessage(q, b'qwe')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'sendMessage', argument 2 of type 'std::string'**

以下是所附文件:

ipc.i文件

%module ipc
%begin %{
#define SWIG_PYTHON_STRICT_BYTE_CHAR
#include "ipc.h"    
%}
%include std_string.i
%include stl.i
%include "ipc.h"

ipc.h文件

#include<string>
extern int createQueue();
extern bool sendMessage(int, std::string);
extern std::string receiveMessage(int);
extern bool removeQueue(int);

用于构建的命令

>> swig -c++ -python ipc.i
>> g++ -fpic -c ipc.h ipc_wrap.cxx ipc.cpp -I/usr/include/python3.5
>> gcc -shared ipc_wrap.o ipc.o -o _ipc.so -lstdc++

SWIG 3.0.8出现问题,更新到3.0.10后问题得到解决。