Upload
leo-zhou
View
147
Download
0
Embed Size (px)
Citation preview
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
● Derek 呆 ○ [email protected] ○ [email protected]
● 來⾃自 Linux Deepin ● 慣 C ● 愛 Python ● 搞 BASH ● 編 Linux System Program
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
準備⼯工作
$ apt-get install build-essential \ gobject-introspection \ python-gobject \ libglib2.0-dev \ gtk-doc-tools
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 1
/* mylib.h */ #ifndef __MYLIB_H_ #define __MYLIB_H_ void hello(); #endif /* __MYLIB_H_ */
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 1
/* mylib.c */ #include "mylib.h" #include <stdio.h> void hello() { printf("hello\n"); }
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 1 $ gcc -o libmylib.so -share … $ g-ir-scanner -o MyLib-1.0.gir \
-lmylib \ -L. \ mylib.h …
$ g-ir-compiler MyLib-1.0.gir >MyLib-1.0.typelib
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 1 ... <namespace name="MyLib" version="1.0" shared-library="libmylib.so" c:identifier-prefixes="MyLib" c:symbol-prefixes="my_lib"> <function name="hello" c:identifier="hello"> <return-value transfer-ownership="none"> <type name="none" c:type="void"/> </return-value> </function> </namespace> ...
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 1
$ python -c 'from gi.repository import MyLib; MyLib.hello()' hello
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
否則 Resource Leaking, Double Free, Invalid FD, Bus Error, Segfault...
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 2
/* mylib.h */ #ifndef __MYLIB_H_ #define __MYLIB_H_ const char * hello3(); char * hello2(); #endif /* __MYLIB_H_ */
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 2 /* mylib.c */ #include "mylib.h" #include <stdio.h> #include <string.h> const char * hello3() { return "hello"; } char * hello2() { return strdup("hello"); }
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 2 ... <function name="hello2" c:identifier="hello2"> <return-value transfer-ownership="full"> <type name="utf8" c:type="char*"/> </return-value> </function> <function name="hello3" c:identifier="hello3"> <return-value transfer-ownership="none"> <type name="utf8" c:type="const char*"/> </return-value> </function> ...
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 2
$ python -c 'from gi.repository import MyLib; print MyLib.hello2(); print MyLib.hello3()' hello hello
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 3
#ifndef __MYLIB_H_ #define __MYLIB_H_ /**
* hello4: * Returns: (transfer full): */ const char * hello4(); #endif /* __MYLIB_H_ */
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 3
#include "mylib.h" #include <stdio.h> const char * hello4() { return "hello"; }
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 3
python -c 'from gi.repository import MyLib; print MyLib.hello4()' *** Error in `python': munmap_chunk(): invalid pointer: 0x00007f4503efd65d *** Aborted
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 4
/** * hello5: * * @msg: (out callee-allocates): */ void hello5(char **msg);
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 4
void hello5(char **msg) { *msg = strdup("hello"); }
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 4
$ python -c 'from gi.repository import MyLib; print MyLib.hello5()' hello
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 5
#include <glib-object.h> #define TYPE_MY_OBJ (my_obj_get_type()) typedef struct _MyObj MyObj; MyObj my_obj_new(); GType my_obj_get_type();
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 5
struct _MyObj { int v; }; MyObj * my_obj_new() { return malloc(sizeof(MyObj)); }
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 5
MyObj * _my_obj_copy(MyObj *self) { MyObj *new_self = my_obj_new(); *new_self = *self; return new_self; }
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 5 GType my_obj_get_type() { static GType type = 0; if(! type) { type = g_boxed_type_register_static( "MyObj", (GBoxedCopyFunc) _my_obj_copy, (GBoxedFreeFunc) _my_obj_free); } return type; }
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
例 5
$ python >>> from gi.repository import MyLib >>> o = MyLib.MyObj() >>> o <MyObj at 0x27d39c0> >>> o.copy() <MyObj at 0x2a7b7e0> >>> o = None >>>
北京/上海/⼲⼴广州 0xFF Life's pathetic, go Pythonic!
廣告時間
● Linux Deepin, 專注桌⾯面體驗的桌⾯面系統 ● 深之度公司⽂文化 ○ 低調, 做好實事 ○ ⽀支持開源⼈人, 做想做的開源事 ○ 週五 Hacking Day 讓你 Hack ⾃自我
● 總部位於武漢 ● 提供遠程⼯工作機會等你來挑戰