自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

講述C++中調(diào)用Python腳本

開(kāi)發(fā) 后端
C++中調(diào)用Python腳本的意義就不講了,至少你可以把它當(dāng)成文本形式的動(dòng)態(tài)鏈接庫(kù),為此我也苦惱很久,后來(lái)終于讓我找到了解決辦法。

也許大家對(duì)與Python腳本還不是很了解,看完本文后對(duì)您一定會(huì)大有幫助,下文除了學(xué)習(xí)Python腳本的基本性質(zhì)外還對(duì)調(diào)用Python腳本時(shí)出現(xiàn)的問(wèn)題進(jìn)行全面研究。

需要的時(shí)候還可以改一改,只要不改變接口, C++的程序一旦編譯好了,再改就沒(méi)那么方便了 先看Python腳本代碼

  1. #test function  
  2.  
  3. def add(a,b):  
  4.  
  5.     print "in python function add"  
  6.  
  7.     print "a = " + str(a)  
  8.  
  9.     print "b = " + str(b)  
  10.  
  11.     print "ret = " + str(a+b)  
  12.  
  13.     return  
  14.  
  15.  
  16.  
  17. def foo(a):  
  18.  
  19.     print "in python function foo"  
  20.  
  21.     print "a = " + str(a)  
  22.  
  23.     print "ret = " + str(a * a)  
  24.  
  25.     return 

把上面的PPython腳本代碼存為pytest.py接下來(lái)是c++ 的代碼:

  1. #include "Python.h"  
  2.  
  3. int main(int argc, char** argv)  
  4. {  
  5.     // 初始化Python  
  6.     //在使用Python系統(tǒng)前,必須使用Py_Initialize對(duì)其  
  7.     //進(jìn)行初始化。它會(huì)載入Python的內(nèi)建模塊并添加系統(tǒng)路  
  8.     //徑到模塊搜索路徑中。這個(gè)函數(shù)沒(méi)有返回值,檢查系統(tǒng)  
  9.     //是否初始化成功需要使用Py_IsInitialized。  
  10.  
  11.     Py_Initialize();  
  12.  
  13.     // 檢查初始化是否成功  
  14.     if ( !Py_IsInitialized() )   
  15.     {  
  16.         return -1;  
  17.     }  
  18.  
  19.     // 添加當(dāng)前路徑  
  20.     //把輸入的字符串作為Python代碼直接運(yùn)行,返回0  
  21.     //表示成功,-1表示有錯(cuò)。大多時(shí)候錯(cuò)誤都是因?yàn)樽址? 
  22.     //中有語(yǔ)法錯(cuò)誤。  
  23.     PyRun_SimpleString("import sys");  
  24.     PyRun_SimpleString("sys.path.append('./')");  
  25.     PyObject *pName,*pModule,*pDict,*pFunc,*pArgs;  
  26.  
  27.     // 載入名為pytest的腳本  
  28.     pName = PyString_FromString("pytest");  
  29.     pModule = PyImport_Import(pName);  
  30.     if ( !pModule )  
  31.     {  
  32.         printf("can't find pytest.py");  
  33.         getchar();  
  34.         return -1;  
  35.     }  
  36.     pDict = PyModule_GetDict(pModule);  
  37.     if ( !pDict )   
  38.     {  
  39.         return -1;  
  40.     }  
  41.  
  42.     // 找出函數(shù)名為add的函數(shù)  
  43.     pFunc = PyDict_GetItemString(pDict, "add");  
  44.     if ( !pFunc || !PyCallable_Check(pFunc) )  
  45.     {  
  46.         printf("can't find function [add]");  
  47.         getchar();  
  48.         return -1;  
  49.     } 

編譯選項(xiàng), 需要手動(dòng)指定Python腳本的include 路徑, 和鏈接接路徑。

【編輯推薦】

  1. 如何使Python嵌入C++應(yīng)用程序?
  2. 深入探討Ruby與Python語(yǔ)法比較
  3. Python學(xué)習(xí)資料介紹分享
  4. Python學(xué)習(xí)經(jīng)驗(yàn)談:版本、IDE選擇及編碼解 決方案
  5. 淺析Python的GIL和線程安全
責(zé)任編輯:chenqingxiang 來(lái)源: 清華大學(xué)出版社
相關(guān)推薦

2010-01-21 13:33:44

C++基類

2010-02-04 17:16:33

C++調(diào)用python

2010-02-06 09:53:26

C++ void

2010-01-12 10:40:22

C++程序員

2010-01-19 15:36:02

C++語(yǔ)言

2010-03-26 13:28:59

Python腳本

2010-01-12 10:11:36

學(xué)習(xí)C++語(yǔ)言

2010-01-13 13:58:49

C++編譯模式

2010-01-12 15:24:48

C++語(yǔ)言

2010-02-04 15:51:07

C++迭代器

2010-02-05 10:08:55

C++名字空間

2019-08-28 14:21:39

C++C接口代碼

2020-07-31 18:33:56

C++編程語(yǔ)言

2010-02-01 14:07:12

C++多態(tài)性

2010-02-04 15:58:39

C++淺拷貝

2010-01-13 11:14:06

C++虛表

2010-02-05 17:34:37

C++函數(shù)模板

2023-03-15 15:58:11

Python動(dòng)態(tài)庫(kù)C++

2010-01-13 11:02:50

C++環(huán)境

2010-01-13 10:45:44

Visual C++
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)