cglibmeson-buildgobject

Why am I getting undefined reference errors when using GLib's G_DEFINE_INTERFACE in C?


I'm trying to follow the "How to define and implement interfaces" guide with a minimal codebase just for the purpose of learning GObject inheritance. So far, I've written a header and an implementation file for my "Fruit" interface that's largely based on what I read in the guide:

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);
}

I also have a main.c file, but its contents is not relevant to this question so I'm omitting it for the sake of brevity.

Anyway, when I try to compile this, I unfortunately get errors:

[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.

To the best of my understanding, these errors don't seem to be my fault as the names (g_type_check_instance_is_a, g_type_interface_peek, g_type_register_static_simple) sound like something GLib itself should be providing given that they all start with g_type and don't mention the name of the type I have defined. So, what should I do about these errors? I'm building this with Meson and my meson.build files look like this:

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,
)

For what it's worth, I'm using the Freedesktop 24.08 SDK as build environment.


Solution

  • You need a dependency on gobject-2.0 as well.