gpt4 book ai didi

c++ - 使用 boost python 和 python 3.2 的 Hello world

转载 作者:太空狗 更新时间:2023-10-29 21:21:26 25 4
gpt4 key购买 nike

所以我尝试使用 boost python 连接 python 3.2 和 c++,并且遇到了很多很多问题。我终于让它可以使用 2.7 库进行编译并且它可以工作,但我似乎无法让它与 python 3.2 一起工作。

这是c++代码

#include <iostream>

using namespace std;

void say_hello(const char* name) {
cout << "Hello " << name << "!\n";
}

int main(){return 0;}

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

BOOST_PYTHON_MODULE(hello)
{
def("say_hello", say_hello);
}

如果我使用 2.7 库编译它,它工作得很好,但是当我使用 3.2 库时,我从 libboost_python.so 得到大量 undefined reference

否则我写了一点 python 来让它工作:

from distutils.core import setup
from distutils.extension import Extension

setup(name="PackageName",
ext_modules=[
Extension("hello", ["testBoost.cpp"],
libraries = ["boost_python"])
])

这将使用 python 3.2 或 2.7 构建创建一个 so,但是当我打开 python 3 解释器并尝试导入 so 时,它再次给我错误 undefined symbol PyClass_Type from libboost_python.so。有任何想法吗? boost python 与 python 3.x 兼容吗?

如果信息有用,这是我尝试使用 3.2 进行编译:

   $ g++ testBoost.cpp -I/usr/include/python3.2 -I/usr/local/include/boost/python -lboost_python -lpython3.2mu 
/tmp/ccdmU1Yu.o: In function `PyInit_hello':
testBoost.cpp:(.text+0xc2): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())'
/usr/local/lib/libboost_python.so: undefined reference to `PyString_Size'
/usr/local/lib/libboost_python.so: undefined reference to `PyFile_FromString'
/usr/local/lib/libboost_python.so: undefined reference to `PyString_Type'
/usr/local/lib/libboost_python.so: undefined reference to `PyInt_Type'
/usr/local/lib/libboost_python.so: undefined reference to `PyString_FromString'
/usr/local/lib/libboost_python.so: undefined reference to `PyString_FromStringAndSize'
/usr/local/lib/libboost_python.so: undefined reference to `Py_InitModule4_64'
/usr/local/lib/libboost_python.so: undefined reference to `PyString_FromFormat'
/usr/local/lib/libboost_python.so: undefined reference to `PyNumber_Divide'
/usr/local/lib/libboost_python.so: undefined reference to `PyNumber_InPlaceDivide'
/usr/local/lib/libboost_python.so: undefined reference to `PyInt_AsLong'
/usr/local/lib/libboost_python.so: undefined reference to `PyString_InternFromString'
/usr/local/lib/libboost_python.so: undefined reference to `PyClass_Type'
/usr/local/lib/libboost_python.so: undefined reference to `PyString_AsString'
/usr/local/lib/libboost_python.so: undefined reference to `PyInt_FromLong'
/usr/local/lib/libboost_python.so: undefined reference to `PyFile_AsFile'
collect2: ld returned 1 exit status

Python 3 解释器的错误是

File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/libboost_python.so.1.47.0: undefined symbol: PyClass_Type

感谢您的帮助!

最佳答案

我在 Ubuntu 12.04 上遇到了完全相同的问题。我安装了 1.48 版本的库,必须链接 libboost_python-py32.so 而不是 libboost_python.so 之后,链接器错误消失了。

关于c++ - 使用 boost python 和 python 3.2 的 Hello world,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7195959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com