導航:首頁 > 編程語言 > lboostpython

lboostpython

發布時間:2023-09-19 01:31:15

① 如何編譯C++文件為python擴展模塊

大概有三種常用方法:
1>使用ctypes模塊來調用C寫的共享庫,比如:
[python] view plain print?
#測試ctypes調用linux動態庫的能力
from ctypes import *
lib = CDLL("libc.so.6")
printf = lib.printf
printf("Hello World\n")

#查找動態庫
from ctypes.util import find_library
print find_library('c')
output = CDLL(find_library("c")).printf
output("測試成功!\n")
但是用它來調用C++寫的so就不太合適,因為編譯時c++函數名修飾會給你的函數取一個特殊的字元串,你不能在你的python代碼里直接使用此函數名,除非你使用的是修飾後的函數名。(在linux下你可以用nm來查看so中的函數名)

2>用C來寫python的擴展模塊,這個沒怎麼用過,以後使用時再記錄在此。
3>用C++來寫python擴展模塊:
我是使用Boost.Python來寫擴展的,先上工作中的代碼片段:

[python] view plain print?
#include <boost/python.hpp> //包含boost.python頭文件
#include <cstdio>
#include <string>

using namespace boost::python;//引入命令空間

class lshw //定義一個類
{
public:
lshw();
virtual ~lshw();

void scan_device();
string get_xml();

private:
hwNode *computer;
};

lshw::lshw()
{
computer = new hwNode("computer", hw::system);
}

lshw::~lshw()
{
if (computer)
delete computer;
}

void lshw::scan_device()
{
enable("output:numeric");
disable("output:sanitize");
scan_system(*computer);
}

string lshw::get_xml()
{
return computer->asXML();
}

void hello()
{
std::cout << "Hello World!" <<std::endl;
}

BOOST_PYTHON_MODULE(lshw)
{
class_<lshw, boost::nonable > ("lshw", "This is a lshw project python extend", init<>())//導出類中的方法
.def("scan_device", &lshw::scan_device)
.def("get_xml", &lshw::get_xml)
;
def("hello",&hello);//導出方法
}
使用boost.python其實結構很簡單,你只要寫很少的boost.python的代碼,你可以把大部分的精力放在C++功能代碼上,花很少的精力就可以把它擴展成python的模塊。下面是我在Ubuntu11.10上的編譯過程:

首先安裝boost.python:
sudo apt-get install libboost-python1.46.1
再來編譯生成so共享庫文件:
g++ -shared -fPIC lshw.cc -o lshw.so -lboost_python
使用:
[python] view plain print?
import lshw
hw = lshw.lshw()
lshw.hello()
hw.scan_device()
xml = self.hw.get_xml()

② python調用c函數

如果有一堆\0,你怎麼知道這個指針指向的內容什麼時候結束?比如應該讀取10個位元組,還是100個位元組。
最起碼要加一個標示指針所指內容長度的參數。

閱讀全文

與lboostpython相關的資料

熱點內容
最好的python編譯器 瀏覽:187
安卓手機如何調分屏 瀏覽:729
安卓系統藍牙耳機如何用 瀏覽:719
為什麼微信不能給appstore充值 瀏覽:493
程序員的保護動物 瀏覽:272
程序員遇到問題去哪個網站 瀏覽:531
安卓手機空格鍵連續輸入怎麼取消 瀏覽:520
壓縮空氣管道流量計 瀏覽:564
ug編程高級教程 瀏覽:177
什麼叫做伺服器已滿 瀏覽:37
暑假哪有教演算法的 瀏覽:136
密碼學的根基是加密 瀏覽:662
stata方差檢驗命令 瀏覽:337
解壓後文件夾里的內容丟失 瀏覽:715
解壓無敵視頻 瀏覽:690
什麼是伺服器辨認不了 瀏覽:129
java如何調用類方法 瀏覽:483
管理孩子的app叫什麼 瀏覽:546
壓縮活動軌跡 瀏覽:674
6米梁加密筋 瀏覽:79