本文目錄一覽:
- 1、linux C語言調用Python腳本?
- 2、c語言調用python有哪些好處
- 3、C語言程序如何調用python程序
- 4、python支持C語言語法嗎?
- 5、c如何調用python程序
- 6、怎樣讓Python腳本與C++程序互相調用
linux C語言調用Python腳本?
比如什麼變量呢?
可以用命令行參數啊
system(“python xxx.py arg1 arg2 …”)
如果讓python接收參數自己查一下
c語言調用python有哪些好處
python是腳本語言,簡潔,易用,可以幫助你寫一些很方便的小程序,庫也豐富,不需要c那麼大規模複雜,所以,有些東西交給腳本語言做,速度快,花費時間少
C語言程序如何調用python程序
下面是一個例子:
首先是python的一個簡單函數
class Hello:
def __init__(self, x):
self.a = x
def print(self, x=None):
print(x)
def xprint():
print(“hello world”)
if __name__ == “__main__”:
xprint()
h = Hello(5)
h.print()1
下面是C語言
#include python3.4m/Python.h
#include stdio.h
#include stdlib.h
#include string.h
int main()
{
Py_Initialize();
// 將當前目錄加入sys.path
PyRun_SimpleString(“import sys”);
PyRun_SimpleString(“sys.path.append(‘./’)”);
// 導入hello.py模塊
PyObject *pmodule = PyImport_ImportModule(“hello”);
// 獲得函數xprint對象,並調用,輸出「hello world\n」
PyObject *pfunc = PyObject_GetAttrString(pmodule, “xprint”);
PyObject_CallFunction(pfunc, NULL);
// 獲得類Hello並生成實例pinstance,並調用print成員函數,輸出「5 6\n」
PyObject *pclass = PyObject_GetAttrString(pmodule, “Hello”);
PyObject *arg = Py_BuildValue(“(i)”, 5);
PyObject *pinstance = PyObject_Call(pclass, arg, NULL);
PyObject_CallMethod(pinstance, “print”, “i”, 6);
Py_Finalize();
return 0;
}
編譯命令如下:
gcc pyapi.c -lpython3.4m -o pyapi
python支持C語言語法嗎?
支持C語言語法,但要你自己實現語法分析器。python的模塊導入機制中,可以自定義一些動作,以支持不同語法的導入。
但一般情況沒有必要這樣做,python語法已經夠好了,沒有必要改成C語法。
CPython就是用C語言開發的python平台。
還有Jyphon是用JAva實現的Python。
還有一個更牛PyPy是用python實現的python。據說用pypy實現的服務器可以支持同時連接100萬個客戶端。
c如何調用python程序
C語言如何調用python,相關步驟如下:
首先,C語言中調用python,要使用頭文件Python.h。
2、接着,定義一個調用python的函數。
相關推薦:《Python教程》
3、函數中,設置python庫的路徑。
4、然後,初始化python。
5、運行一個python代碼,輸出How are you。
6、最後,釋放python。
怎樣讓Python腳本與C++程序互相調用
二、Python調用C/C++
1、Python調用C動態鏈接庫
Python調用C庫比較簡單,不經過任何封裝打包成so,再使用python的ctypes調用即可。
(1)C語言文件:pycall.c
[html] view plain copy
/***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 copy
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 copy
#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:”aendl;
}
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 copy
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 copy
#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=”numendl;
cout”—end—“endl;
}
(2)編譯成二進制可執行文件:g++ -o testmain main.cpp。
(3)Python調用程序:main.py
[html] view plain copy
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 copy
#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* Module_func()的包裝函數;c、為每個模塊增加一個型如PyMethodDef ModuleMethods[]的數組;d、增加模塊初始化函數void initModule()。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/234122.html