膠水語言Python與C/C++的相互調(diào)用的實現(xiàn)
準(zhǔn)備工作:
python:https://www.python.org/downloads/
Dev-C++:https://sourceforge.net/projects/orwelldevcpp/
gcc和g++:http://mingw-w64.org/doku.php
notepad++:https://notepad-plus.en.softonic.com/
一、Python調(diào)用C
步驟1:Csayhello.c
#include<stdio.h>
void show_hello()
{
printf("------------來自C語言的問候-----------\n");
printf("-----Peter Zhao says:Hello C world!-----\n\n");
}
步驟2:
命令:gcc Csayhello.c -fPIC -shared -o lib_Csayhello.so
步驟3:Psayhello.py
from ctypes import *
#加載動態(tài)庫
lib = cdll.LoadLibrary(r"./lib_Csayhello.so")
lib.show_hello()
print("-----------來自Python語言的問候--------------")
print("---Peter Zhao says:Hello Python world,too!---")
步驟4:
命令:python Psayhello.py
注意:python為32位,沒有就裝一個。
運行結(jié)果:

二、Python調(diào)用C++
步驟1:新建項目dll_demo.dev
步驟2:dllmain.cpp
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int multiply(int a, int b) {
return a * b;
}
//兩數(shù)相加
DLLEXPORT int add(int a, int b) {
return a + b;
}
//兩數(shù)相減
DLLEXPORT int sub(int a, int b) {
return a-b;
}
步驟3:dll.h
int multiply(int, int);
class Mymath {
int sum(int, int);
int sub(int, int);
};
步驟4:編譯生成dll_demo.dll
步驟5:Pdll_demo.py
import ctypes #lib = ctypes.cdll.LoadLibrary(r"./dll_demo.dll") lib = ctypes.WinDLL(r"./dll_demo.dll") #print(lib) print(lib.multiply(80,95)) print(lib.add(80,95)) print(lib.sub(80,95))
步驟6:
命令:python Pdll_demo.py
注意:python為32位,沒有就裝一個。
運行結(jié)果:

三、C++調(diào)用Python函數(shù)
步驟1:Caculate.py
def add(a,b):
return a+b
步驟2:新建項目test.dev,然后設(shè)置一下“項目屬性”的鏈接庫、庫目錄、包含文件目錄等3個部分。



步驟3:test.cpp
#include <python.h>
#include<iostream>
using namespace std;
int main()
{
Py_Initialize();//使用python之前,要調(diào)用Py_Initialize();這個函數(shù)進行初始化
if (!Py_IsInitialized())
{
printf("初始化失??!");
return 0;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");//這一步很重要,修改Python路徑
PyObject * pModule = NULL;//聲明變量
PyObject * pFunc = NULL;// 聲明變量
pModule = PyImport_ImportModule("Caculate");//這里是要調(diào)用的文件名Caculate.py
if (pModule==NULL)
{
cout << "沒找到" << endl;
}
pFunc = PyObject_GetAttrString(pModule, "add");//這里是要調(diào)用的函數(shù)名
PyObject* args = Py_BuildValue("(ii)", 100, 120);//給python函數(shù)參數(shù)賦值
PyObject* pRet = PyObject_CallObject(pFunc, args);//調(diào)用函數(shù)
int res = 0;
PyArg_Parse(pRet,"i",&res);//轉(zhuǎn)換返回類型
cout << "res:" << res << endl;//輸出結(jié)果
Py_Finalize();//調(diào)用Py_Finalize,這個根Py_Initialize相對應(yīng)的。
return 0;
}
步驟4:編譯并運行
運行結(jié)果:

到此這篇關(guān)于膠水語言Python與C/C++的相互調(diào)用的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python與C/C++相互調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中TypeError: unhashable type: ‘list‘錯誤的解
在Python編程的領(lǐng)域中,數(shù)據(jù)類型的正確使用是確保程序正常運行的關(guān)鍵要素之一,然而,開發(fā)者們常常會遇到一些由于數(shù)據(jù)類型使用不當(dāng)而引發(fā)的報錯,其中TypeError: unhashable type: 'list’就是一個比較典型的錯誤,那么,讓我們深入探究這個報錯問題,為大家提供全面的解決方案2024-10-10
python中的decimal類型轉(zhuǎn)換實例詳解
decimal 模塊實現(xiàn)了定點和浮點算術(shù)運算符,使用的是大多數(shù)人所熟悉的模型,而不是程序員熟悉的模型,即大多數(shù)計算機硬件實現(xiàn)的 IEEE 浮點數(shù)運算。這篇文章主要介紹了python里的decimal類型轉(zhuǎn)換,需要的朋友可以參考下2019-06-06
Python Pytorch深度學(xué)習(xí)之自動微分
今天小編就為大家分享一篇關(guān)于Pytorch自動微分的文章,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-10-10
基于pytorch的RNN實現(xiàn)字符級姓氏文本分類的示例代碼
當(dāng)使用基于PyTorch的RNN實現(xiàn)字符級姓氏文本分類時,我們可以使用一個非常簡單的RNN模型來處理輸入的字符序列,并將其應(yīng)用于姓氏分類任務(wù),本文給大家舉了一個基本的示例代碼,需要的朋友可以參考下2023-12-12

