提升:1.60.0
python:3.5.0.1(来自anaconda)

样例代码:

#include <iostream>
#include <boost/python.hpp>
#include <Python.h>

namespace py = boost::python;

int main()
{
    // Must be called before any boost::python functions
    Py_Initialize();
    // import the main module
    py::object main_module = py::import("__main__");
    // load the dictionary object out of the main module
    py::object main_namespace = main_module.attr("__dict__");
    // run simple code within the main namespace using the boost::python::exec
    //  function
    py::exec("print ('Hello, world')", main_namespace);
    // any valid Python will execute
    py::exec("print ('Hello, world')[3:5]", main_namespace);
}


编译为:
g++ -std=c++11 test.cpp -o test.o -I/home/user/anaconda3/pkgs/python-3.5.1-0/include/python3.5m/ -L/home/user/anaconda3/pkgs/python-3.5.1-0/lib/python3.5/config-3.5m -lpython3.5m -pthread -lutil -ldl -lboost_python3

输出:

//usr/local/lib/libboost_python3.so: undefined reference to `PyString_AsString'
//usr/local/lib/libboost_python3.so: undefined reference to `PyInt_Type'
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_FromStringAndSize'
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_FromString'
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_FromFormat'
//usr/local/lib/libboost_python3.so: undefined reference to `PyInt_FromLong'
//usr/local/lib/libboost_python3.so: undefined reference to `PyInt_AsLong'
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_Type'


在此之前,我还遇到其他链接器错误,我正在与2.7版本的python链接。我用3.5版本构建了boost_python

我了解这些功能确实在3.5中不存在;不知道在哪里使用它们。

相反,请在下面找到:

:~/$ nm /usr/local/lib/libboost_python3.so |grep "PyString"
                 U PyString_AsString
                 U PyString_FromFormat
                 U PyString_FromString
                 U PyString_FromStringAndSize
                 U PyString_Type
:~/$ nm /usr/local/lib/libboost_python3.so |grep "PyInt"
                 U PyInt_AsLong
                 U PyInt_FromLong
                 U PyInt_Type


我怎样才能简单地“前进”?

实际上,我尝试在C ++文件中定义这些函数,因为我完全知道我不会调用这些函数,但是它不起作用。我知道这是一种有趣的方式...,但是我想提一个想法,我想“继续前进”。

最佳答案

我认为您的libboost_python3.so适用于python 2.7或3.1之前的python版本,因为它引用的是例如PyString_AsString在python 3.1、3.2、3.3、3.4或3.5中不可用(基于在debian软件包中的库中找到的符号) 。

nm -D /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0|grep PyString_AsString
0000000000112120 T PyString_AsString
0000000000111fd0 T PyString_AsStringAndSize


和python 3.4(与3.5相同)

nm -D /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0 |grep AsString
000000000017ea10 T PyByteArray_AsString
000000000017f680 T PyBytes_AsString
0000000000180cf0 T PyBytes_AsStringAndSize

07-25 20:39