GLib程序编译错误

时间:2013-05-29 05:43:50

标签: glib gobject

我正在尝试学习glib编程,以便在使用g_object_new()创建对象时查看流程,但在编译时遇到此错误,需要一些提示,我在使用G_DEFINE_TYPE(第25行)做错了什么?

gcc -Wall `pkg-config --cflags --libs glib-2.0` loop.c -o loop
loop.c: In function ‘network_listener_get_type’:
loop.c:25: error: invalid application of ‘sizeof’ to incomplete type ‘NetworkListenerClass’ 
loop.c: In function ‘main’:
loop.c:60: warning: unused variable ‘nl’

代码是:

#include <stdio.h>
#include <glib.h>
#include <glib-object.h>

#define NETWORK_LISTENER_TYPE   (network_listener_get_type())
#define NETWORK_LISTENER(obj)   (G_TYPE_CHECK_INSTANCE_CAST ((obj), NETWORK_LISTENER_TYPE, NetworkListener))

typedef struct _NetworkListener NetworkListener;
typedef struct _NetworkListenerCLass NetworkListenerClass;


 struct _NetworkListener
 {
   GObject parent_instance;
   unsigned int ipAddr;
   int port;
 };

 struct _NetworkListenerClass
 {
   GObjectClass parent_class;
 };

 G_DEFINE_TYPE(NetworkListener, network_listener, G_TYPE_OBJECT);

 static GObject * network_listener_constructor(GType gtype,
                              guint nprops,
                              GObjectConstructParam *props)
 {
    GObject *obj;
    printf("network listener constructor called\n");
    {
      obj = G_OBJECT_CLASS(network_listener_parent_class)->constructor(gtype, nprops, props);
    }
    return obj;
 }

 static void network_listener_class_init(NetworkListenerClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
   printf("network listener class init called\n");
   gobject_class->constructor = network_listener_constructor;
 }

 static void network_listener_init(NetworkListener *self)
 {
   printf("Called network_listener_init\n");
                                                                    1,28          Top
 }

 gboolean callback(gpointer data)
 {
   printf("%s\n", (char*)data);
   return TRUE;
 }

 int main()
 {
   GMainLoop* main_loop = NULL;
   NetworkListener *nl = g_object_new(NETWORK_LISTENER_TYPE, NULL);
   main_loop = g_main_loop_new (NULL, FALSE);
/* interval, function callback, userdata */
   g_timeout_add_seconds(10, callback, "callback_function");
   g_main_loop_run(main_loop);
   return 0;
 }

1 个答案:

答案 0 :(得分:1)

这是NetworkListenerClass typedef中的拼写错误...有一个不应该有的大写字母L(你有“CLass”而不是“Class”)。