首页 > 解决方案 > gcc 5.2.0 的 boost.python 字符串兼容性

问题描述

接受std::string输入的函数无法通过带有 gcc/5.2.0 的 boost.python 正确公开;而 gcc 4.8.3 工作正常。

ps 我使用的是预建的 libboost_python.so.1.63。

C++ 代码

#include <boost/python.hpp>
#include <iostream>
using namespace boost::python;

void func(const std::string & hi)
{
    std::cout<<hi<<std::endl;
}

BOOST_PYTHON_MODULE(example)
{
  def("func", &func);
}

Python 执行

>>> from example import *
>>> func("hi")
Boost.Python.ArgumentError: Python argument types in
    example.func(str)
did not match C++ signature:
    func(std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> >)

标签: pythonc++boost-python

解决方案


推荐阅读