c - 为什么 G_DEFINE_INTERFACE 会触发 "expected declaration specifiers"编译器错误?

标签 c glib gobject

作为熟悉 GObject 的一部分,我正在尝试按照 the example in the reference manual 创建一个“Hello, world”界面。这是我在 hello_world_if.h 中的内容:

#ifndef __HELLO_WORLD_IF_H__
#define __HELLO_WORLD_IF_H__

#include <glib-object.h>

G_BEGIN_DECLS

#define TYPE_HELLO_WORLD_IF                (hello_world_if_get_type())
#define HELLO_WORLD_IF(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_HELLO_WORLD_IF, HelloWorldIf))
#define IS_HELLO_WORLD_IF(obj)             (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_HELLO_WORLD_IF))
#define HELLO_WORLD_IF_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE((inst), TYPE_HELLO_WORLD_IF, HelloWorldIfInterface))

typedef struct _HelloWorldIf HelloWorldIf; /* dummy object */
typedef struct _HelloWorldIfInterface HelloWorldIfInterface;

struct _HelloWorldIfInterface {
  GTypeInterface parent;

  gchar *(*get_hello)(HelloWorldIf *self);
};

GType hello_world_if_get_type(void);

gchar *hello_world_if_get_hello(HelloWorldIf *self);

G_END_DECLS

#endif /* __HELLO_WORLD_IF_H__ */

以及hello_world_if.c :

#include "hello_world_if.h"

G_DEFINE_INTERFACE(HelloWorldIf, hello_world_if, 0);

static void
hello_world_if_default_init(gpointer g_class) {
  /* Add properties and signals to the interface here */
}

gchar *
hello_world_if_get_hello(HelloWorldIf *self) {
  g_return_if_fail(IS_HELLO_WORLD_IF(self));

  HELLO_WORLD_IF_GET_INTERFACE(self)->get_hello(self);
}

但这不能编译:

$ make
gcc -g -pthread -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include   -c -o hello_world_if.o hello_world_if.c
hello_world_if.c:3: error: expected declaration specifiers or ‘...’ before ‘hello_world_if’
hello_world_if.c:3: error: expected declaration specifiers or ‘...’ before numeric constant
hello_world_if.c:3: warning: data definition has no type or storage class
make: *** [hello_world_if.o] Error 1

从阅读这里的其他答案看来,这个“预期声明说明符”消息通常意味着 a necessary header file hasn't been includedhas been included too late 。但我不确定这里怎么会出现这种情况。 (具体来说,将 #include <glib.h>#include <glib-object.h> 添加到 C 文件不会改变任何内容。)

我一定错过了一些简单的东西,但我只是没有看到它。帮忙?

最佳答案

结果有一个简单的解释:G_DEFINE_INTERFACE 宏是 added in GLib 2.24.0 ,但我使用的是版本 2.22.5(CentOS 6.3 上的标准)。我需要构建并安装更新版本的 GLib,或者挖掘旧的引用文档 — the website不会回溯到 2.26.1 之后。

关于c - 为什么 G_DEFINE_INTERFACE 会触发 "expected declaration specifiers"编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14176810/

相关文章:

c - 用C代码编写二进制数字系统

c++ - Glib::RefPtr 超出范围后的 SIGSEGV?

c - GObject 样本 : undefined reference to `product_finalize' ?

c - OCaml 的 MIT-SHM 绑定(bind)

c - MPI:rand() 在每次运行中为所有进程提供相同的常数

c - C 中的数据存储被多个文件使用

linux - 可以动态创建 gsettings 吗?

c++ - GLib-GObject-CRITICAL ...断言 'quark > 0' 失败

python - GObject.add_emission_hook 只能工作一次

xml - 如何在 GtkBuilder 的 XML *.ui 文件中使用 GTK+3 GtkImage