『壹』 如何實現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 */
}

『貳』 如何實現 C/C++ 與 Python 的通信

屬於混合編程的問題。較全面的介紹一下,不僅限於題主提出的問題。

以下討論中,Python指它的標准回實現,即CPython(雖然答不是很嚴格)

本文分4個部分

C/C++ 調用 Python (基礎篇)— 僅討論Python官方提供的實現方式
Python 調用 C/C++ (基礎篇)— 僅討論Python官方提供的實現方式
C/C++ 調用 Python (高級篇)— 使用 Cython
Python 調用 C/C++ (高級篇)— 使用 SWIG

練習本文中的例子,需要搭建Python擴展開發環境。具體細節見搭建Python擴展開發環境 - 蛇之魅惑 - 知乎專欄

1 C/C++ 調用 Python(基礎篇)
Python 本身就是一個C庫。你所看到的可執行體python只不過是個stub。真正的python實體在動態鏈接庫里實現,在Windows平台上,這個文件位於 %SystemRoot%\System32\python27.dll。

『叄』 python與c++混合編程如何入門

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

『肆』 如何發布C和python混合編程的程序

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

『伍』 python和c語言混編的幾種方式

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

『陸』 python和c++混編cmake怎麼寫

please reload systemd manually or reboot the system 提示信息里不是說讓你重新載入systemd嗎? 如果手工載入不上,重啟也不行回,請確認一下,是否安裝了答systemd.

『柒』 python怎麼和C或者C++混合編程

不難的,就是一個套路,主要是3步:
1.把Python的數據類型轉換為C/C++支持的數據類型;
2.調用C/C++函數,得到結果;
3.將結果轉換為Python支持的數據類型,返回。
相當於寫個中間層。

『捌』 Python和c語言有什麼區別

前者為 腳本語言 需要虛擬機 解釋執行 ..一般情況下 不能生成exe(要使用打包工具)

後者為 為編譯型語言 ..可以直接轉換為 二進制 代碼 ...執行速度 也相對較快 ..

兩者可以混合編程

『玖』 python和什麼語言混編

python是C語言編寫的一門
高級語言
,支持和原生C語言
混編
,甚至可以直接添加C代碼執行。