gpt4 book ai didi

python - 使用 boost python : introductory example not working 从 python 调用 C++

转载 作者:行者123 更新时间:2023-11-30 02:34:08 24 4
gpt4 key购买 nike

我有兴趣使用 Boost.Python 从我的 Python 调用 C++ 函数脚本。

这个例子here是我无法运行的 Boost python 主页上的介绍性示例。有人可以帮我解决这个问题吗?

这是我尝试过的

我创建了一个名为hello_ext.cpp的文件如下

#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}


BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}

然后我把它编译成共享库如下

g++ -c -Wall -Werror -fpic hello_ext.cpp -I/usr/include/python2.7
g++ -shared -o libhello_ext.so hello_ext.o

最后启动 ipython 解释器,我尝试import hello_ext但收到以下错误消息。我哪里出错了?

In [1]: import hello_ext
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-18c4d6548768> in <module>()
----> 1 import hello_ext

ImportError: ./hello_ext.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

最佳答案

你应该在你的链接命令中包含一些库,

 g++ -shared -Wl,--no-undefined hello_ext.o -lboost_python -lpython2.7 -o hello_ext.so

使用 -Wl,--no-undefined 链接器选项,如果缺少某些符号,将会出错。

关于python - 使用 boost python : introductory example not working 从 python 调用 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801850/

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