在函數(shù)聲明加入前綴,如
根河網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,根河網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為根河千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的根河做網(wǎng)站的公司定做!
__declspec(dllexport) int Fun(int a, int b)
否則在加載該dll時會提示找不到該符號
在windows下可以通過vs自帶的dumpbin工具查看可被調(diào)用符號
dumpbin /exports test.dll
C函數(shù)在調(diào)用過程中關(guān)于參數(shù)傳遞和壓棧由多種規(guī)定,作為dll提供給其他程序調(diào)用時,必須明確并統(tǒng)一為同一種調(diào)用規(guī)定,否則會導(dǎo)致棧破壞,編譯器負責(zé)具體實現(xiàn)調(diào)用規(guī)定,主要有以下幾種調(diào)用規(guī)定
python下調(diào)用C庫有多種方式,ctypes是其中一種比較方便的,調(diào)用時首先需要加載dll文件,根據(jù)C dll的調(diào)用規(guī)定不同需要使用不同接口,使用ctypes需要 import ctypes 庫
對于簡單的C函數(shù),例如 int add(int a, int b) , 此時就可以直接調(diào)用了,如
對于較復(fù)雜的C函數(shù)的參數(shù)情況,ctypes調(diào)用時對入?yún)⒑统霾妥鲆欢ㄌ幚恚@里分情況討論
以上包含了幾種主要的參數(shù)傳遞情況,ctypes也提供了一個較為完整的python類型和C類型的對照,如下:
二、Python調(diào)用C/C++\x0d\x0a\x0d\x0a\x0d\x0a1、Python調(diào)用C動態(tài)鏈接庫\x0d\x0a\x0d\x0a Python調(diào)用C庫比較簡單,不經(jīng)過任何封裝打包成so,再使用python的ctypes調(diào)用即可。\x0d\x0a(1)C語言文件:pycall.c\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a/***gcc -o libpycall.so -shared -fPIC pycall.c*/ \x0d\x0a#include \x0d\x0a#include \x0d\x0aint foo(int a, int b) \x0d\x0a{ \x0d\x0a printf("you input %d and %d\n", a, b); \x0d\x0a return a+b; \x0d\x0a} \x0d\x0a(2)gcc編譯生成動態(tài)庫libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++編譯生成C動態(tài)庫的代碼中的函數(shù)或者方法時,需要使用extern "C"來進行編譯。\x0d\x0a(3)Python調(diào)用動態(tài)庫的文件:pycall.py\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0aimport ctypes \x0d\x0all = ctypes.cdll.LoadLibrary \x0d\x0alib = ll("./libpycall.so") \x0d\x0alib.foo(1, 3) \x0d\x0aprint '***finish***' \x0d\x0a(4)運行結(jié)果:\x0d\x0a\x0d\x0a\x0d\x0a2、Python調(diào)用C++(類)動態(tài)鏈接庫 \x0d\x0a\x0d\x0a 需要extern "C"來輔助,也就是說還是只能調(diào)用C函數(shù),不能直接調(diào)用方法,但是能解析C++方法。不是用extern "C",構(gòu)建后的動態(tài)鏈接庫沒有這些函數(shù)的符號表。\x0d\x0a(1)C++類文件:pycallclass.cpp\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a#include \x0d\x0ausing namespace std; \x0d\x0a \x0d\x0aclass TestLib \x0d\x0a{ \x0d\x0a public: \x0d\x0a void display(); \x0d\x0a void display(int a); \x0d\x0a}; \x0d\x0avoid TestLib::display() { \x0d\x0a cout \x0d\x0ausing namespace std; \x0d\x0aint test() \x0d\x0a{ \x0d\x0a int a = 10, b = 5; \x0d\x0a return a+b; \x0d\x0a} \x0d\x0aint main() \x0d\x0a{ \x0d\x0a cout \x0d\x0a#include \x0d\x0a#include \x0d\x0a \x0d\x0aint fac(int n) \x0d\x0a{ \x0d\x0a if (n
回答于?2022-11-16
Python是解釋性語言, 底層就是用c實現(xiàn)的, 所以用python調(diào)用C是很容易的, 下面就總結(jié)一下各種調(diào)用的方法, 給出例子, 所有例子都在ubuntu9.10, python2.6下試過
1. Python 調(diào)用 C (base)
想在python中調(diào)用c函數(shù), 如這兒的fact
#include Python.h
int fact(int n)
{
if (n = 1)
return 1;
else
return n * fact(n - 1);
}
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
int n, result;
if (! PyArg_ParseTuple(args, "i:fact", n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
{NULL, NULL}
};
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
把這段代碼存為wrapper.c, 編成so庫,
gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
然后在有此so庫的目錄, 進入python, 可以如下使用
import example
example.fact(4)
2. Python 調(diào)用 C++ (base)
在python中調(diào)用C++類成員函數(shù), 如下調(diào)用TestFact類中的fact函數(shù),
#include Python.h
class TestFact{
public:
TestFact(){};
~TestFact(){};
int fact(int n);
};
int TestFact::fact(int n)
{
if (n = 1)
return 1;
else
return n * (n - 1);
}
int fact(int n)
{
TestFact t;
return t.fact(n);
}
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
int n, result;
if (! PyArg_ParseTuple(args, "i:fact", n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
{NULL, NULL}
};
extern "C" //不加會導(dǎo)致找不到initexample
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
把這段代碼存為wrapper.cpp, 編成so庫,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
然后在有此so庫的目錄, 進入python, 可以如下使用
import example
example.fact(4)
3. Python 調(diào)用 C++ (Boost.Python)
Boost庫是非常強大的庫, 其中的python庫可以用來封裝c++被python調(diào)用, 功能比較強大, 不但可以封裝函數(shù)還能封裝類, 類成員.
首先在ubuntu下安裝boost.python, apt-get install libboost-python-dev
#include boost/python.hpp
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
把代碼存為hello.cpp, 編譯成so庫
g++ hello.cpp -o hello.so -shared -I/usr/include/python2.5 -I/usr/lib/python2.5/config -lboost_python-gcc42-mt-1_34_1
此處python路徑設(shè)為你的python路徑, 并且必須加-lboost_python-gcc42-mt-1_34_1, 這個庫名不一定是這個, 去/user/lib查
然后在有此so庫的目錄, 進入python, 可以如下使用
import hello
hello.greet()
'hello, world'
4. python 調(diào)用 c++ (ctypes)
ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.
ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.
#include Python.h
class TestFact{
public:
TestFact(){};
~TestFact(){};
int fact(int n);
};
int TestFact::fact(int n)
{
if (n = 1)
return 1;
else
return n * (n - 1);
}
extern "C"
int fact(int n)
{
TestFact t;
return t.fact(n);
}
將代碼存為wrapper.cpp不用寫python接口封裝, 直接編譯成so庫,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
進入python, 可以如下使用
import ctypes
pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')
pdll.fact(4)
12
分享標(biāo)題:c與python調(diào)用函數(shù) python 調(diào)用c語言
URL標(biāo)題:http://vcdvsql.cn/article36/dooocpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站收錄、App設(shè)計、電子商務(wù)、云服務(wù)器、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)