【问题标题】:Including a gobject.h in codeblocks在代码块中包含 gobject.h
【发布时间】:2013-08-18 16:24:11
【问题描述】:

我想在 Codeblocks 中使用 C GObject lib 来上课。所以我有 2 个课程:A.h 和 A.c 但是当我像这样包含 gobject lib 时:

#include <glib-2.0/gobject/gobject.h>

它给了我一个错误:

 Only <glib-object.h> can be included directly.

因为这个文件包含以下几行:

 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
 #error "Only <glib-object.h> can be included directly."
 #endif

啊哈:

#ifndef A_H
#define A_H
#include <glib-2.0/gobject/gobject.h>
//It's only declaring an class called A

typedef struct _AClass AClass;
typedef struct _A A;

struct _AClass {
     //Always Derived from GObjectClass class
     GObjectClass parent_instance;
}

struct _A{
   //Always Derived from GObject class
   GObject parent_instance;
}

#endif // A_H

交流:

#include "/home/alex/GObject_Tutorial/include/A.h"
//It's defining a class called A

G_DEFINE_TYPE(A, aObj, G_TYPE_OBJECT);

我认为包含这些库的唯一方法是将它们添加到本地文件夹并包含类似(但也许还有另一种方法):

#include "gobject.h"

【问题讨论】:

    标签: c codeblocks gobject


    【解决方案1】:

    谢谢。所以我的总答案是: 我们不只包括 gobject/gobject.h,因为 glib-object.h 是聚合器,它包含所有这些库以提供 gobject-class 的创建。 我的步骤:

    1) 使用以下命令查找 glibs 中的库在哪里:

     pkg-config --cflags --libs gobject-2.0
    

    所以,我得到了结果(在你的情况下可能会有所不同):

     -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -lgobject-2.0 -lglib-2.0
    

    2) 然后在code::blocks Project->Build Options->Compiler Settings->Other Options

    3) 在文本字段中复制这一行 (-I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include)

    4) Copy to Project->Build Options->Linker Settings->Other Linker Options这一行(-lgobject-2.0 -lglib-2.0)

    5) 构建

    6) 万岁!成功了,谢谢大家:)

    【讨论】:

      【解决方案2】:

      您应该遵循有用的错误消息,而不是尝试解决它。​​

      您不能在 GLib 之外直接包含 gobject.h:只有 glib-object.h 才能包含在应用程序代码中。

      您还应该在包含规则中使用非系统头文件的相对路径:使用编译器和/或 IDE 中的包含路径设置。

      【讨论】:

        猜你喜欢
        • 2018-07-15
        • 2017-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多