導航:首頁 > 編程語言 > python調用c混合編程

python調用c混合編程

發布時間:2022-04-19 13:34:19

1. python調用c語言編譯

如何讓python調用C和C++代碼

安裝python後,會有一個chm格式的python手冊。要搞明白如何讓python調用C/C++代碼(也就是寫python的 extension),你需要征服手冊中的
<<Extending && embedding>>厚厚的一章。在昨天花了一個小時看地頭暈腦脹,仍然不知道如何寫python的extension後,查閱了一些其他 書籍,最終在<<Python Programming On Win32>>書中找到了教程。
下面記錄一下如何在visual studio 2005中,寫一段C/C++的MessageBox代碼,然後提供後python調用,最後的結果當然是顯示一個MessageBox.
1. 首先要明白的是,所謂的python擴展(也就是你提供給python的c/c++代碼,不一定是c/c++代碼,可以是其他語言寫的代碼)是一個 dll,並且這個dll放在本機python安裝目錄下的DLLs目錄下(譬如我機器上的路徑是:F:\Program Files\Python25\DLLs),假如我們接下來要寫的擴展mole名為mb,python調用的代碼為: import mb
mb.showMsg("Python's really amazing, I kindda love it!")
python怎麼找到我們的mb模塊呢?就是上面說的,我們要生成一個mb.dll,然後拷貝到Dlls目錄下面,為了區別普通的dll和python專用擴展的dll,我們的 mb.dll修改成mb.pyd(python dll)
2. 搭建環境,我們要使用python提供的c頭文件和lib庫來進行擴展的開發。 在vs 2005下點擊菜單 "工具"->"選項", 打開選項對話框,選擇"項目和解決方案->VC++目錄", 然後在右邊"顯示以下內容的目錄"得comboBox上選擇"包含文件」,添加python的include目錄(我的機器上是"F:\Program
Files\Python25\include"),然後選擇庫文件,添加python的libs目錄(我的機器上是"F:\Program Files\Python25\libs")。
既然擴展是一個dll,接下來我們要建立一個「動態鏈接庫」工程,然後開始寫代碼:
#include <python.h> //python.h是包含python一些定義的頭文件,在python的include目錄下 /*
我的python版本是2.5, 因為安裝python後它沒提供debug下的lib庫文件,因此你必須生成release版的dll,
想要生成dll版本的,你要到python官網上自己去下載python源代碼,當然你可以繼續生成release版本的dll,但dll中包含調試信息

2. python 怎麼調用c語言介面

ctypes: 可直接調用c語言動態鏈接庫。

使用步驟:

1> 編譯好自己的動態連接庫
2> 利用ctypes載入動態連接庫
3> 用ctype調用C函數介面時,需要將python變數類型做轉換後才能作為函數參數,轉換原則見下圖:

#Step1:test.c#include<stdio.h>

intadd(inta,intb)
{
returna+b;
}#Step2:編譯動態鏈接庫(如何編譯動態鏈接庫在本文不詳解,網上資料一大堆。)gcc-fPIC-sharedtest.c-olibtest.so
#Step3:test.py
fromctypesimport*mylib=CDLL("libtest.so")或者cdll.LoadLibrary("libtest.so")add=mylib.add
add.argtypes=[c_int,c_int]#參數類型,兩個int(c_int是ctypes類型,見上表)
add.restype=c_int#返回值類型,int(c_int是ctypes類型,見上表)
sum=add(3,6)

3. 如何在python中調用C語言代碼

先把C語言代碼做成DLL文件,再用python載入此DLL文件來訪問C語言功能代碼

4. 如何實現C與python混合編程

實現C與python混合編程方法
代碼如下:

/* tcpportping.c */
#include <Python.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>

/* count time functions */
static double mytime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1)
return 0.0;

return (double)tv.tv_usec + (double)tv.tv_sec * 1000000;
}

static PyObject * /* returns object */
tcpping(PyObject *self, PyObject *args )
{
struct sockaddr_in addr;
struct hostent *hp;
double time;
char *host = NULL;
int fd;
int port, timeout;

if (!PyArg_ParseTuple(args, "sii", &host, &port, &timeout)) /* convert Python -> C */
return NULL; /* null=raise exception */

if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return Py_BuildValue("d", -1.0); /* convert C -> Python */
}

bzero((char *)&addr, sizeof(addr));
if ((hp = gethostbyname(host)) == NULL) {
return Py_BuildValue("d", -2.0); /* convert C -> Python */
}
b(hp->h_addr, &addr.sin_addr, hp->h_length);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);

struct timeval tv;

tv.tv_sec = 0;
tv.tv_usec = timeout * 1000;

double stime = mytime();
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
return Py_BuildValue("d", -3.0); /* convert C -> Python */
}
fd_set read, write;
FD_ZERO(&read);
FD_ZERO(&write);

FD_SET(fd, &read);
FD_SET(fd, &write);

if (select(fd + 1, &read, &write, NULL, &tv) == 0) {
close(fd);
return Py_BuildValue("d", -4.0); /* convert C -> Python */
}

double etime = mytime();
time = etime - stime;
if (!FD_ISSET(fd, &read) && !FD_ISSET(fd, &write)) {
close(fd);
return Py_BuildValue("d", -4.0); /* convert C -> Python */
}
close(fd);
return Py_BuildValue("d", time/1000); /* convert C -> Python */
}

/* registration table */
static struct PyMethodDef portping_methods[] = {
{"tcpping", tcpping, METH_VARARGS}, /* method name, C func ptr, always-tuple */
{NULL, NULL} /* end of table marker */
};

/* mole initializer */
void inittcpportping( ) /* called on first import */
{ /* name matters if loaded dynamically */
(void) Py_InitMole("tcpportping", portping_methods); /* mod name, table ptr */
}

5. 怎樣讓Python腳本與C++程序互相調用

二、Python調用C/C++

1、Python調用C動態鏈接庫

Python調用C庫比較簡單,不經過任何封裝打包成so,再使用python的ctypes調用即可。
(1)C語言文件:pycall.c

[html] view plain
/***gcc -o libpycall.so -shared -fPIC pycall.c*/
#include <stdio.h>
#include <stdlib.h>
int foo(int a, int b)
{
printf("you input %d and %d\n", a, b);
return a+b;
}
(2)gcc編譯生成動態庫libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++編譯生成C動態庫的代碼中的函數或者方法時,需要使用extern "C"來進行編譯。
(3)Python調用動態庫的文件:pycall.py

[html] view plain
import ctypes
ll = ctypes.cdll.LoadLibrary
lib = ll("./libpycall.so")
lib.foo(1, 3)
print '***finish***'
(4)運行結果:

2、Python調用C++(類)動態鏈接庫

需要extern "C"來輔助,也就是說還是只能調用C函數,不能直接調用方法,但是能解析C++方法。不是用extern "C",構建後的動態鏈接庫沒有這些函數的符號表。
(1)C++類文件:pycallclass.cpp

[html] view plain
#include <iostream>
using namespace std;

class TestLib
{
public:
void display();
void display(int a);
};
void TestLib::display() {
cout<<"First display"<<endl;
}

void TestLib::display(int a) {
cout<<"Second display:"<<a<<endl;
}
extern "C" {
TestLib obj;
void display() {
obj.display();
}
void display_int() {
obj.display(2);
}
}
(2)g++編譯生成動態庫libpycall.so:g++ -o libpycallclass.so -shared -fPIC pycallclass.cpp。
(3)Python調用動態庫的文件:pycallclass.py

[html] view plain
import ctypes
so = ctypes.cdll.LoadLibrary
lib = so("./libpycallclass.so")
print 'display()'
lib.display()
print 'display(100)'
lib.display_int(100)
(4)運行結果:

3、Python調用C/C++可執行程序
(1)C/C++程序:main.cpp

[html] view plain
#include <iostream>
using namespace std;
int test()
{
int a = 10, b = 5;
return a+b;
}
int main()
{
cout<<"---begin---"<<endl;
int num = test();
cout<<"num="<<num<<endl;
cout<<"---end---"<<endl;
}
(2)編譯成二進制可執行文件:g++ -o testmain main.cpp。
(3)Python調用程序:main.py

[html] view plain
import commands
import os
main = "./testmain"
if os.path.exists(main):
rc, out = commands.getstatusoutput(main)
print 'rc = %d, \nout = %s' % (rc, out)

print '*'*10
f = os.popen(main)
data = f.readlines()
f.close()
print data

print '*'*10
os.system(main)
(4)運行結果:

4、擴展Python(C++為Python編寫擴展模塊)
所有能被整合或導入到其它python腳本的代碼,都可以被稱為擴展。可以用Python來寫擴展,也可以用C和C++之類的編譯型的語言來寫擴展。Python在設計之初就考慮到要讓模塊的導入機制足夠抽象。抽象到讓使用模塊的代碼無法了解到模塊的具體實現細節。Python的可擴展性具有的優點:方便為語言增加新功能、具有可定製性、代碼可以實現復用等。
為 Python 創建擴展需要三個主要的步驟:創建應用程序代碼、利用樣板來包裝代碼和編譯與測試。
(1)創建應用程序代碼

[html] view plain
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int fac(int n)
{
if (n < 2) return(1); /* 0! == 1! == 1 */
return (n)*fac(n-1); /* n! == n*(n-1)! */
}

char *reverse(char *s)
{
register char t, /* tmp */
*p = s, /* fwd */
*q = (s + (strlen(s) - 1)); /* bwd */

while (p < q) /* if p < q */
{
t = *p; /* swap & move ptrs */
*p++ = *q;
*q-- = t;
}
return(s);
}

int main()
{
char s[BUFSIZ];
printf("4! == %d\n", fac(4));
printf("8! == %d\n", fac(8));
printf("12! == %d\n", fac(12));
strcpy(s, "abcdef");
printf("reversing 'abcdef', we get '%s'\n", \
reverse(s));
strcpy(s, "madam");
printf("reversing 'madam', we get '%s'\n", \
reverse(s));
return 0;
}
上述代碼中有兩個函數,一個是遞歸求階乘的函數fac();另一個reverse()函數實現了一個簡單的字元串反轉演算法,其主要目的是修改傳入的字元串,使其內容完全反轉,但不需要申請內存後反著復制的方法。
(2)用樣板來包裝代碼
介面的代碼被稱為「樣板」代碼,它是應用程序代碼與Python解釋器之間進行交互所必不可少的一部分。樣板主要分為4步:a、包含Python的頭文件;b、為每個模塊的每一個函數增加一個型如PyObject* Mole_func()的包裝函數;c、為每個模塊增加一個型如PyMethodDef MoleMethods[]的數組;d、增加模塊初始化函數void initMole()。

6. python和C語言混編的幾種方式

C語言設計一個完整的可執行文件,然後python通過subprocess來執行該可執行文件,本質上是fork+execve。
C語言使用popen/system或者直接以系統調用級fork+exec來運行python程序也是一種混編的手段了。

7. python與c++混合編程如何入門

派桑和c/c++實際上是不同的工具,一個是斧頭,一個是菜刀,各有各的用途。非要把讓菜刀能砍柴,讓斧頭能切菜,這種努力有無必要,值得考慮。
派桑常用劇本,嚕蘇,速度慢,不嚴謹,但有它的便利,例如它有 list, tuple, 之類的東西。
混合編程 常用於 提高 派桑 速度。
常用 方法 是(1)包含 Python.h 文件頭 (2)定義 派桑 的 各種對象 為 各種 c 結構
(3)派桑 的 各種對象用指針,名字用 Py 或 _Py大頭 動態分配 在 heap (4)用函數跟蹤統計 各種對象結構 的個數變化,即時 釋放內存(5)及時處理 派桑 exception
(6) 輸出為派桑的數據格式。
我沒有合起來用過,不過基本套路如此。

8. python 調用C可執行程序

比如參數是ARG1,ARG2,輸入字元串是TEST

import subprocess as sub
p = sub.Popen(['alignment ARG1 ARG2'], shell = True, stdin = sub.PIPE, stdout = sub.PIPE, stderr = sub.PIPE)
(out,err) = p.communicate(input = 'TEST')

out是執行後的標准輸出

「問題補充:那個C可執行程序需要CTRL + C 才能退出,就不知道該怎麼辦了」

可以發送SIGINT信號給可執行程序
Popen.send_signal(signal)

等同於ctrl + c

閱讀全文

與python調用c混合編程相關的資料

熱點內容
c523壓縮比 瀏覽:543
命令語氣的人什麼心態 瀏覽:435
程序員喜歡留指甲嗎 瀏覽:516
七牛雲伺服器收費標准 瀏覽:627
時光相冊加密空間密碼忘記 瀏覽:474
華為雲為用戶提供的服務雲伺服器 瀏覽:634
minecraftlinux伺服器搭建 瀏覽:376
linux命令新建文件 瀏覽:708
長線pdf 瀏覽:607
程序員電腦支持手寫 瀏覽:414
解壓頭戴式耳機推薦 瀏覽:344
紙條app上怎麼樣看對方主頁 瀏覽:883
編譯英語單詞怎麼寫 瀏覽:249
編譯原理和匯編原理的區別 瀏覽:864
如何給加密的pdf解密 瀏覽:770
華為盒子時間同步伺服器地址 瀏覽:95
python處理excel亂碼 瀏覽:391
mysql的命令行 瀏覽:822
jpeg採用什麼演算法 瀏覽:701
程序員紅軸薄膜 瀏覽:306