日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

多个cpp文件生成so_boostpython:从多个.cpp文件创建一个模块(.so)

發(fā)布時間:2023/12/4 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多个cpp文件生成so_boostpython:从多个.cpp文件创建一个模块(.so) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我開始在C++中編寫一些我想在Python代碼中調(diào)用的模塊。為此,我使用boostpython。隨著代碼的增長,我決定將其分成幾個.cpp文件。現(xiàn)在,我有了這樣的東西:

食品.cpp#include "Bar.hpp"

#include "Baz.hpp"

#include

using namespace boost::python;

BOOST_PYTHON_MODULE(foo){

def("help", help);

class_("Bar", init<:list>())

.def("add", &Bar::add)

.def("show", &Bar::show);

}

酒吧cpp

^{pr2}$

巴水電站#ifndef Bar_hpp

#define Bar_hpp

#include

#include

#include

namespace bp=boost::python;

class Bar{

private:

std::vector<:string> items;

public:

Bar(bp::list list);

~Bar();

void add(std::string x);

bp::list show();

};

#endif

巴茲.cpp#include

#include

#include

namespace bp=boost::python;

//C++ vector to Python list

template

bp::list toPythonList(std::vector vector) {

typename std::vector::iterator iter;

bp::list list;

for (iter = vector.begin(); iter != vector.end(); ++iter) {

list.append(*iter);

}

return list;

}

//Python list to C++ int vector

std::vector toCppVectorInt(bp::list list){

std::vector vector;

for (int i=0;i !=bp::len(list) ;i++){

vector.push_back(bp::extract((list)[i]));

}

return vector;

}

//Python list to C++ string vector

std::vector<:string> toCppVectorStr(bp::list list){

std::vector<:string> vector;

for (int i=0;i !=bp::len(list) ;i++){

vector.push_back(bp::extract<:string>((list)[i]));

}

return vector;

}

std::string help(){

return "some help information ";

}

巴茲水電站#ifndef Baz_hpp

#define Baz_hpp

#include

#include

#include

namespace bp=boost::python;

template

bp::list toPythonList(std::vector vector);

//Python list to C++ int vector

std::vector toCppVectorInt(bp::list list);

//Python list to C++ string vector

std::vector<:string> toCppVectorStr(bp::list list);

std::string help();

#endif

我的設(shè)置.py文件:from distutils.core import setup

from distutils.extension import Extension

setup(name="PackageName", ext_modules=[Extension("foo",["foo.cpp"],

libraries = ["boost_python"])

])

編譯時使用:python setup.py build

我得到文件福索(編譯無錯誤地終止)。不幸的是,它不起作用。當(dāng)我試圖在Python解釋器中使用它時,我收到了這樣的效果:Python 2.7.6 (default, Jun 22 2015, 17:58:13)

[GCC 4.8.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import foo

Traceback (most recent call last):

File "", line 1, in

ImportError: ./foo.so: undefined symbol: _ZN3NNB3addESs

>>>

我應(yīng)該在代碼中更正什么?在

總結(jié)

以上是生活随笔為你收集整理的多个cpp文件生成so_boostpython:从多个.cpp文件创建一个模块(.so)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。