我试图在介子的帮助下构建这个例子: Vala 中的 libpeas 扩展入门
我的 meson.build 如下所示:
vala_deps = [
dependency('gio-2.0', version: '>= 2.50'),
dependency('gtk+-3.0', version: '>= 3.22'),
]
libpeas_dep = dependency('libpeas-1.0')
libfoo_deps = [vala_deps, libpeas_dep]
libfoo = shared_library(
'foo',
'window.vala',
vala_header: 'foo.h',
dependencies: libfoo_deps,
install: true,
)
libfoo_dep = declare_dependency(
link_with: libfoo,
dependencies: libfoo_deps,
include_directories: include_directories('.'),
)
extension_deps = [vala_deps, libpeas_dep]
extension = shared_library(
'extension',
'extension.vala',
vala_header: 'extension.h',
dependencies: [vala_deps, libpeas_dep, libfoo_dep],
install: true,
#install_dir : '/app/bin'
)
extension_dep = declare_dependency(
link_with: extension,
dependencies: extension_deps,
include_directories: include_directories('.'),
)
gnome = import('gnome')
extension_resources = gnome.compile_resources(
'extension-resources',
'extension.gresource.xml',
c_name: 'extension',
)
foo_sources = [
'main.vala',
extension_resources[0],
]
foo = executable(
'foo',
foo_sources,
link_with: extension,
#objects: 'libextension.so',
dependencies: [vala_deps, libfoo_dep, extension_dep],
install: true,
)
objects: 'libextension.so',
如果我在示例中使用预编译的共享对象文件foo = executable(
,如果我不这样做,它不会。
有什么建议么?
编辑:
涉及的文件内容如下:
扩展名.gresource.xml:
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gnome/Foo/plugins">
<file>extension.plugin</file>
</gresource>
</gresources>
扩展插件:
[Plugin]
Name=extension
Module=extension
Embedded=peas_register_types
扩展名.vala:
class ValaExtension : Object, Foo.Extension {
public Foo.Window window { get; construct set; }
Gtk.Button button;
void activate() {
button = new Gtk.Button.with_label("Say Hello");
button.clicked.connect(() => {
button.set_label("Hello World!");
});
window.buttons.add(button);
button.show();
}
void deactivate() {
window.buttons.remove(button);
}
}
[ModuleInit]
public void peas_register_types(TypeModule module) {
var objmodule = module as Peas.ObjectModule;
objmodule.register_extension_type(typeof (Foo.Extension), typeof (ValaExtension));
}
main.vala:
int main (string[] args) {
var app = new Gtk.Application ("org.gnome.Foo", ApplicationFlags.FLAGS_NONE);
app.activate.connect (() => {
var win = app.active_window;
if (win == null) {
win = new Foo.Window (app);
}
win.present ();
});
return app.run (args);
}
窗口.vala:
namespace Foo {
public class Window : Gtk.Window {
public Gtk.ButtonBox buttons { get; set; }
private Peas.ExtensionSet extensions { get; set; }
public Window(Gtk.Application app) {
Object(application: app);
this.buttons = new Gtk.ButtonBox(Gtk.Orientation.VERTICAL);
this.destroy.connect(Gtk.main_quit);
var engine = Peas.Engine.get_default();
engine.add_search_path("/app/lib", null);
engine.prepend_search_path("resource:///org/gnome/Foo/plugins/", null);
extensions = new Peas.ExtensionSet(engine, typeof (Foo.Extension), "window", this);
extensions.extension_added.connect((info, extension) => {
(extension as Foo.Extension).activate();
});
extensions.extension_removed.connect((info, extension) => {
(extension as Foo.Extension).deactivate();
});
foreach (var plugin in engine.get_plugin_list())
engine.try_load_plugin(plugin);
this.add(buttons);
this.show_all();
}
}
public interface Extension : Object {
public abstract Window window { get; construct set; }
public abstract void activate();
public abstract void deactivate();
}
}
如果我评论该objects:'libextension.so'
行,我会收到以下错误:
(foo:2): libpeas-WARNING **: 20:27:31.400: Failed to get 'peas_register_types' for module 'extension': 'peas_register_types': foo: undefined symbol: peas_register_types
(foo:2): libpeas-WARNING **: 20:27:31.400: Error loading plugin 'extension'
解决了:
如果我添加一个虚拟函数:
public void extension_init ()
{
}
到extension.vala并从main.vala调用它,链接适用于嵌入式插件,另请参见:gnome_builder_plugins_init.c