使用Boost Python将C ++函数扩展到Python

Prit1123

我正在尝试包装要在Python中使用的两个c ++文件。我正在使用boost python库。文件似乎可以正确编译,但是导入模块会导致“ ImportError:未定义符号”错误。

这个问题与boost没有正确找到依赖的c ++文件有关,但是我不清楚如何添加它们。

Python版本:2.7.12 Boost版本:1.58操作系统:Ubuntu 16.04

我的代码结构如下:

hellomodule.cpp

#include <iostream>
#include <cstdint>
#include "test.h"

using namespace std;

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

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

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

测试文件

#include "test.h"
using namespace std;

void run_test(void){
    cout << "Sup";
}

setup.py

#!/usr/bin/env python
from distutils.core import setup
from distutils.extension import Extension

module1 = Extension("hello",
                    sources = ["hellomodule.cpp", "test.cpp"],
                    libraries = ["boost_python"],
                    extra_compile_args=['-std=c++11'])

setup(name="PackageName",
    ext_modules=[module1])

从命令行运行“ python setup.py build”,该文件创建了hello.so文件。当我尝试导入“ hello”时,出现“ ImportError:./hello.so:undefined symbol:_Z8run_testv”

如果有人能指出我正确的方向,将不胜感激。

鲈鱼

似乎您周围可能有一些过时的文件。我能够通过省略重现该问题test.cpp,从sourcessetup.py在这种情况下,它的构建就很好,但是正如您所观察到的那样,它不会导入。可能是Pythonhello.so在添加之前找到了先前构建的版本test.cpp

我建议删除该build目录,然后删除该目录的所有副本,hello.so然后尝试再次从头开始运行构建。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Pybind11将C ++扩展到Python

将列表扩展到Python中的json文件

使用集群将Socket.IO扩展到多个Node.js进程

将Python搜索路径扩展到其他来源

用Python方法将列表元素作为单个实际参数扩展到函数(如matlab)?

将Python嵌入C ++并使用Boost.Python从C ++代码调用方法

使用扩展将Swift类扩展到多个文件

将元素扩展到.pop中使用的列表

Bash将$ *或$ @扩展到函数

如何使用Builder将PageView扩展到双方?

使用dplyr将开始和结束日期扩展到不平衡的每月面板

使用edittext将字符串从mainactivity扩展到扩展AsyncTask的活动

使用boost-python将参数从Python脚本传递到C ++

语言:如何使用泛型将抽象类扩展到两个级别

使用TailwindCSS将元素的高度扩展到页面底部

将DatetimeIndex扩展到Python熊猫中的行

使用/ boot将分区扩展到左侧后,Syslinux“ Missing OS”

使用BOOST.python从C ++将结构返回到Python

Python将dict值扩展到列表

Java Object使用Jackson将列表扩展到Json

使用自定义CSS将标题扩展到Wordpress.com主题上的全角

使用Boost :: Python包装纯虚函数

使用boost :: python将虚拟成员函数从C ++公开给Python

使用reduce将源对象的可枚举属性扩展到目标对象

将 str 作为 int 数组传递给 Python C 扩展函数(使用 SWIG 扩展)

如何使用 Moore 和 Von Nuemman 示例中的代码将邻居函数扩展到 24

如何使用动画将容器在颤动中缓慢扩展到一定高度?

如何将 Pandas 函数从一列函数扩展到多列函数

如何将范围扩展到 exec 函数?