0

我试图在介子的帮助下构建这个例子: 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

4

1 回答 1

0

objects: 'libextension.so'将对象直接链接到可执行文件。从示例中看起来好像您正在使用 libpeas 在运行时加载模块。因此,通过将模块构建到可执行文件中,我假设您在运行时加载模块时遇到问题。

本教程还建议设置LD_LIBRARY_PATH为包含带有扩展名的目录并还有一个 libpeas.plugin文件。这可能是问题所在。如果您列出错误消息或不起作用的详细信息,这将有所帮助。

顺便说一句,使用介子构建目标中的对象可能会更好,这样objects: extension.extract_objects()对象的名称不会硬编码到构建中。请参阅构建目标对象。的使用objects:主要用于测试。

更新

我认为您的模块名称extension.plugin应该是extension.so. 我也喜欢明确说明我过去使用过的模块加载器Loader=C。我以前没有遇到过Embedded。所以我会试试这个作为我的extension.plugin

[Plugin]
Name=My first libpeas extension
Module=extension.so
Loader=C
于 2018-08-08T08:47:29.137 回答