使用GLib的G_DEFINE_INTERFACE为何出现未定义引用错误?
问题描述
我正在学习GObject接口定义,编写了一个最小化的Fruit接口代码,包含头文件fruit.h和实现文件fruit.c:
fruit.h
#pragma once #include <glib-object.h> G_BEGIN_DECLS #define EXAMPLE_TYPE_FRUIT example_fruit_get_type () G_DECLARE_INTERFACE (ExampleFruit, example_fruit, EXAMPLE, FRUIT, GObject) struct _ExampleFruitInterface { GTypeInterface parent_iface; char *flavour; void (*eat) (ExampleFruit *self); }; void example_fruit_eat (ExampleFruit *self); G_END_DECLS
fruit.c
#include "fruit.h" #include <stdio.h> G_DEFINE_INTERFACE (ExampleFruit, example_fruit, G_TYPE_OBJECT) static void example_fruit_default_init (ExampleFruitInterface *iface) { // Nothing to do? } void example_fruit_eat (ExampleFruit *self) { ExampleFruitInterface *iface; g_return_if_fail (EXAMPLE_IS_FRUIT (self)); iface = EXAMPLE_FRUIT_GET_IFACE (self); printf ("Non nom, so %s\n", iface->flavour); }
编译时出现链接错误,错误信息如下:
[3/4] Linking target src/gobject-inheritance FAILED: src/gobject-inheritance cc -o src/gobject-inheritance src/gobject-inheritance.p/fruit.c.o src/gobject-inheritance.p/main.c.o -L/app/lib -Wl,--as-needed -Wl,--no-undefined /usr/lib/x86_64-linux-gnu/libglib-2.0.so /usr/lib/gcc/x86_64-unknown-linux-gnu/14.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld: src/gobject-inheritance.p/fruit.c.o: in function `EXAMPLE_IS_FRUIT': /mnt/storage/Programming/.gnome-builder/projects/gobject-inheritance/builds/org.gnome.Example.json-flatpak-org.freedesktop.Platform-24.08-x86_64-main/../../../../../gobject-inheritance/src/fruit.h:8:(.text+0x61): undefined reference to `g_type_check_instance_is_a' /usr/lib/gcc/x86_64-unknown-linux-gnu/14.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld: src/gobject-inheritance.p/fruit.c.o: in function `EXAMPLE_FRUIT_GET_IFACE': /mnt/storage/Programming/.gnome-builder/projects/gobject-inheritance/builds/org.gnome.Example.json-flatpak-org.freedesktop.Platform-24.08-x86_64-main/../../../../../gobject-inheritance/src/fruit.h:8:(.text+0x8f): undefined reference to `g_type_interface_peek' /usr/lib/gcc/x86_64-unknown-linux-gnu/14.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld: src/gobject-inheritance.p/fruit.c.o: in function `example_fruit_get_type': /mnt/storage/Programming/.gnome-builder/projects/gobject-inheritance/builds/org.gnome.Example.json-flatpak-org.freedesktop.Platform-24.08-x86_64-main/../../../../../gobject-inheritance/src/fruit.c:5:(.text+0x127): undefined reference to `g_type_register_static_simple' /usr/lib/gcc/x86_64-unknown-linux-gnu/14.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld: /mnt/storage/Programming/.gnome-builder/projects/gobject-inheritance/builds/org.gnome.Example.json-flatpak-org.freedesktop.Platform-24.08-x86_64-main/../../../../../gobject-inheritance/src/fruit.c:5:(.text+0x140): undefined reference to `g_type_interface_add_prerequisite' collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed.
使用Meson构建,当前配置如下:
根目录meson.build
project('gobject-inheritance', 'c', version: '0.1.0', meson_version: '>= 1.0.0', default_options: [ 'warning_level=2', 'werror=false', 'c_std=gnu11', ], ) cc = meson.get_compiler('c') subdir('src')
src/meson.build
gobject_inheritance_sources = [ 'fruit.c', 'fruit.h', 'main.c', ] gobject_inheritance_deps = [ dependency('glib-2.0'), ] executable('gobject-inheritance', gobject_inheritance_sources, dependencies: gobject_inheritance_deps, install: true, )
构建环境为Freedesktop 24.08 SDK,请问如何解决这些未定义引用错误?
解决方案
这些未定义的函数(g_type_check_instance_is_a、g_type_interface_peek等)属于GObject库,而非基础GLib库。当前仅添加了glib-2.0依赖,缺少gobject-2.0的链接依赖。
修改src/meson.build中的依赖列表,添加gobject-2.0:
gobject_inheritance_deps = [ dependency('glib-2.0'), dependency('gobject-2.0'), ]
或更简洁地直接使用glib-2.0的gobject组件:
gobject_inheritance_deps = dependency('glib-2.0', components: ['gobject'])
重新运行meson setup --reconfigure和ninja即可解决链接错误。
内容的提问来源于stack exchange,提问作者Newbyte
相关产品推荐
相关产品推荐

