test.c(動態(tài)庫源代碼)
創(chuàng)新互聯(lián)科技有限公司專業(yè)互聯(lián)網(wǎng)基礎(chǔ)服務(wù)商,為您提供四川雅安服務(wù)器托管,高防服務(wù)器,成都IDC機(jī)房托管,成都主機(jī)托管等互聯(lián)網(wǎng)服務(wù)。
[cpp] view plain copy
// 編譯生成動態(tài)庫: gcc -g -fPIC -shared -o libtest.so test.c
#include stdio.h
#include string.h
#include stdlib.h
typedef struct StructPointerTest
{
char name[20];
int age;
}StructPointerTest, *StructPointer;
StructPointer test() // 返回結(jié)構(gòu)體指針
{
StructPointer p = (StructPointer)malloc(sizeof(StructPointerTest));
strcpy(p-name, "Joe");
p-age = 20;
return p;
}
編譯:gcc -g -fPIC -shared -o libtest.so test.c
call.py(python調(diào)用C語言生成的動態(tài)庫):
[python] view plain copy
#!/bin/env python
# coding=UTF-8
from ctypes import *
#python中結(jié)構(gòu)體定義
class StructPointer(Structure):
_fields_ = [("name", c_char * 20), ("age", c_int)]
if __name__ == "__main__":
lib = cdll.LoadLibrary("./libtest.so")
lib.test.restype = POINTER(StructPointer)
p = lib.test()
print "%s: %d" %(p.contents.name, p.contents.age)
最后運(yùn)行結(jié)果:
[plain] view plain copy
[zcm@c_py #112]$make clean
rm -f *.o libtest.so
[zcm@c_py #113]$make
gcc -g -fPIC -shared -o libtest.so test.c
[zcm@c_py #114]$./call.py
Joe: 20
[zcm@c_py #115]$
int* GrabImage();
int GetPixel(int* image, int x, int y);
void SetPixel(int* image, int x, int y, int color);
在計算機(jī)程序設(shè)計中,回調(diào)函數(shù),或簡稱回調(diào)(Callback),是指通過函數(shù)參數(shù)傳遞到其它代碼的,某一塊可執(zhí)行代碼的引用。這一設(shè)計允許了底層代碼調(diào)用在高層定義的子程序:
例如:
def?my_callback(input):
print?"function?my_callback?was?called?with?%s?input"?%?(input,)
def?caller(input,?func):
func(input)
for?i?in?range(5):
caller(i,?my_callback)
執(zhí)行結(jié)果是:
function?my_callback?was?called?with?0?input
function?my_callback?was?called?with?1?input
function?my_callback?was?called?with?2?input
function?my_callback?was?called?with?3?input
function?my_callback?was?called?with?4?input