我正在嘗試使用以下IBM tutorial作爲參考創建python綁定到vala庫。vala庫的Python綁定
我最初的目錄有以下兩個文件:
test.vala
using GLib;
namespace Test {
public class Test : Object {
public int sum(int x, int y) {
return x + y;
}
}
}
test.override
%%
headers
#include <Python.h>
#include "pygobject.h"
#include "test.h"
%%
modulename test
%%
import gobject.GObject as PyGObject_Type
%%
ignore-glob
*_get_type
%%
,並嘗試建立了Python模塊源test_wrap.c
使用以下代碼
build.sh
#/usr/bin/env bash
valac test.vala -CH test.h
python /usr/share/pygobject/2.0/codegen/h2def.py test.h > test.defs
pygobject-codegen-2.0 -o test.override -p test test.defs > test_wrap.c
不過,最後的命令失敗,錯誤
$ ./build.sh
Traceback (most recent call last):
File "/usr/share/pygobject/2.0/codegen/codegen.py", line 1720, in <module>
sys.exit(main(sys.argv))
File "/usr/share/pygobject/2.0/codegen/codegen.py", line 1672, in main
o = override.Overrides(arg)
File "/usr/share/pygobject/2.0/codegen/override.py", line 52, in __init__
self.handle_file(filename)
File "/usr/share/pygobject/2.0/codegen/override.py", line 84, in handle_file
self.__parse_override(buf, startline, filename)
File "/usr/share/pygobject/2.0/codegen/override.py", line 96, in __parse_override
command = words[0]
IndexError: list index out of range
這是pygobject一個錯誤,或者是有毛病我的設置?調用python編寫的代碼的最佳方式是什麼?
編輯: 刪除多餘的線固定當前的問題,但現在我着手建立Python模塊,我面對的另一個問題。添加下面的C文件現有的兩個目錄:
test_module.c
#include <Python.h>
void test_register_classes (PyObject *d);
extern PyMethodDef test_functions[];
DL_EXPORT(void)
inittest(void)
{
PyObject *m, *d;
init_pygobject();
m = Py_InitModule("test", test_functions);
d = PyModule_GetDict(m);
test_register_classes(d);
if (PyErr_Occurred()) {
Py_FatalError ("can't initialise module test");
}
}
和建築用下面的腳本
build.sh
#/usr/bin/env bash
valac test.vala -CH test.h
python /usr/share/pygobject/2.0/codegen/h2def.py test.h > test.defs
pygobject-codegen-2.0 -o test.override -p test test.defs > test_wrap.c
CFLAGS="`pkg-config --cflags pygobject-2.0` -I/usr/include/python2.6/ -I."
LDFLAGS="`pkg-config --libs pygobject-2.0`"
gcc $CFLAGS -fPIC -c test.c
gcc $CFLAGS -fPIC -c test_wrap.c
gcc $CFLAGS -fPIC -c test_module.c
gcc $LDFLAGS -shared test.o test_wrap.o test_module.o -o test.so
python -c 'import test; exit()'
導致錯誤:
$ ./build.sh
***INFO*** The coverage of global functions is 100.00% (1/1)
***INFO*** The coverage of methods is 100.00% (1/1)
***INFO*** There are no declared virtual proxies.
***INFO*** There are no declared virtual accessors.
***INFO*** There are no declared interface proxies.
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: ./test.so: undefined symbol: init_pygobject
init_pygobject
符號在哪裏定義?我錯過了什麼鏈接?
這個問題是相當幾年前問,情況現在可能已經改變。現在情況如何?即什麼是** CURRENTLY **爲vala代碼生成Python綁定的最佳方式? – 2012-05-08 08:05:22