glib

  • 简介
  • 命令行参数解析
  • 事件源

简介

  1. 源码下载
  2. glib库是Linux平台下最常用的C语言函数库,它具有很好的可移植性和实用性
  3. 编译链接:gcc g_init.c pkg-config --libs --cflags glib-2.0 -o g_init
  4. 功能特色
    (1)gobject是glib的精粹,gobject是所有类的基类。signal在其中也是一大特色。
    (2)glib提供了动态数组、单/双向链表、哈希表、多叉树、平衡二叉树、字符串等常用容器,完全是面向对象设计的。
    (3)glib里提供了词法分析、markup语言解析、ini文件存取等功能
    (4)glib提供了一套跨平台的backtrace函数
    (5)实现了一套完整的log机制
    (6)glib提供了一些的手段定位内存问题
  5. GLib 实现了一个功能强大的事件循环分发处理机制.
    (1)被抽象成 GMainLoop,用于循环处理事件源上的事件,每个 GMainLoop 都工作在指定的 GMainContext 上。
    (2)事件源在 GLib 中则被抽象成了 GSource。在 GMainContext 中有一个 GSource 列表。
    (3)GLib 内部定义实现了三种类型的事件源,分别是 Idle, Timeout 和 I/O。同时也支持创建自定义的事件源。

命令行参数解析

#include <glib.h>gboolean  cmd_arg_show_version = FALSE;
gint      cmd_arg_product = 0;
gchar    *cmd_arg_name    = NULL;static GOptionEntry entries[] = {{ "version", 'v', 0, G_OPTION_ARG_NONE, &cmd_arg_show_version, "Show version", NULL },{ "product", 'p', 0, G_OPTION_ARG_INT, &cmd_arg_product, "product mode", NULL },{ "name", 'n', 0, G_OPTION_ARG_STRING, &cmd_arg_name, "product name", NULL },{ NULL }
};typedef struct _glib_context {GMainLoop *main_loop;
} glib_context_t;static glib_context_t *glib_context = NULL;int main(int argc, char *argv[])
{GError *error = NULL;GOptionContext *context = g_option_context_new("- argument");g_option_context_add_main_entries(context, entries, NULL);if (!g_option_context_parse(context, &argc, &argv, &error)) {g_printerr("option parsing failed: %s\n", error->message);g_option_context_free(context);exit(EXIT_FAILURE);}g_option_context_free(context);if (cmd_arg_show_version) {g_print("%s\n", "v1.0");exit(EXIT_SUCCESS);}glib_context = (glib_context_t *)g_malloc0(sizeof(glib_context_t));glib_context->main_loop = g_main_loop_new(NULL, FALSE);g_main_loop_run(glib_context->main_loop);if (glib_context->main_loop) {g_main_loop_unref(glib_context->main_loop);glib_context->main_loop = NULL;}g_free(glib_context);glib_context = NULL;return 0;
}

事件源

  1. 事件源: Idle, Timeout, I/O 和 自定义的事件源
  2. 自定义事件源:将外部信号(事件)挂到程序中的指定主循环上,从而在 g_main_loop_run 中可以响应这些事件
    (1)自定义的事件源继承 GSource 的结构体,即自定义事件源的结构体 的第一个成员是 GSource 结构体, 其后便可放置程序所需数据
      [1] sample: struct Test_Source { GSource _source; gchar userdata[256]; }
    (2)实现事件源所规定的接口,它们包含于 GSourceFuncs 结构体中
      [1] 主要为 prepare, check, dispatch, finalize 等事件处理函数(回调函数)
    (3)g_source_new 构造一个新的事件源, g_source_attach 将新的事件源添加到主循环上下文中
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>gboolean  cmd_arg_show_version = FALSE;
gint      cmd_arg_product = 0;
gchar    *cmd_arg_name    = NULL;static GOptionEntry entries[] = {{ "version", 'v', 0, G_OPTION_ARG_NONE, &cmd_arg_show_version, "Show version", NULL },{ "product", 'p', 0, G_OPTION_ARG_INT, &cmd_arg_product, "product mode", NULL },{ "name", 'n', 0, G_OPTION_ARG_STRING, &cmd_arg_name, "product name", NULL },{ NULL }
};typedef struct _glib_context {GMainLoop     *main_loop;GMainContext  *context;
} glib_context_t;static gint            should_exit = 0;
static glib_context_t *glib_context = NULL;static void sig_int_handler(int signo G_GNUC_UNUSED)
{should_exit++;if (should_exit == 1) {if (g_main_loop_is_running(glib_context->main_loop)) {g_main_loop_quit(glib_context->main_loop);g_printf("\n\n sig_int_handler exit ***\n");}}
}void OptionContextParse(int argc, char *argv[])
{GError *error = NULL;GOptionContext *context = g_option_context_new("- argument");g_option_context_add_main_entries(context, entries, NULL);if (!g_option_context_parse(context, &argc, &argv, &error)) {g_printerr("option parsing failed: %s\n", error->message);g_option_context_free(context);exit(EXIT_FAILURE);}g_option_context_free(context);if (cmd_arg_show_version) {g_print("%s\n", "v1.0");exit(EXIT_SUCCESS);}if (cmd_arg_name) {g_print("cmd_arg_name: %s\n", cmd_arg_name);exit(EXIT_SUCCESS);}
}typedef struct _MySource
{GSource source;gchar   text[256];
} MySource;// 判断其返回值以确定各事件源是否作好准备,返回false后会调用check,返回true会执行dispatch函数
static gboolean prepare(GSource *source, gint *timeout)
{*timeout = 1000; /* set time interval one msecond */g_printf("prepare ***\n");return FALSE;
}static gboolean check(GSource *source)
{g_printf("check ***\n");static int i = 0;i++;if (i % 2 == 0) return FALSE;elsereturn TRUE;
}static gboolean dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{MySource *mysource = (MySource *)source;g_print("dispatch: %s\n", mysource->text);return TRUE;
}static void finalize(GSource *source)
{MySource *mysource = (MySource *)source;g_print("finalize: %s\n", mysource->text);
}GSourceFuncs source_funcs = {prepare, check, dispatch, finalize};void glib_self_defined_Source_process(glib_context_t *glib_context)
{glib_context->context = g_main_loop_get_context(glib_context->main_loop);GSource *source = g_source_new(&source_funcs, sizeof(MySource)); // sizeof() 是自定义的长度g_sprintf(((MySource *)source)->text, "glib_Source_process!");// context 如果为局部参数会crash,函数完成后会被销毁,context变为野指针g_source_attach(source, glib_context->context);g_source_unref(source);
}// 返回值:TRUE,主事件循环空闲时重复被执行;
// 返回值:FALSE,执行一次后,被主事件循环从事件源中移除
static gboolean idle_func(gpointer data)
{static int i = 0;i++;if (i % 5 != 0) g_print ("idle_func %d, data %s.\n", i, (gchar *)data);elsereturn FALSE;return TRUE;
}static gboolean timeout_func(gpointer data)
{static int i = 0;i++;if (i % 5 != 0) g_print ("timeout_func %d\n", i);elsereturn FALSE;return TRUE;
}gboolean io_watch(GIOChannel *channel, GIOCondition condition, gpointer data)
{gsize len = 0;gchar *buffer = NULL;g_io_channel_read_line(channel, &buffer, &len, NULL, NULL);if(len > 0)g_print("io_watch: %ld, buffer:%s.\n", len, buffer);g_free(buffer);return TRUE;
}int main(int argc, char *argv[])
{signal(SIGINT, sig_int_handler);OptionContextParse(argc, argv);glib_context = (glib_context_t *)g_malloc0(sizeof(glib_context_t));/* GMainLoop数据类型代表了一个主事件循环, 通过g_main_loop_new()来创建GMainLoop对象,在添加完初始事件源后执行g_main_loop_run(),主循环将持续不断的检查每个事件源产生的新事件,然后分发它们,直到处理来自某个事件源的事件的时候触发了g_main_loop_quit()调用退出主循环为止。*/glib_context->main_loop = g_main_loop_new(NULL, FALSE);if (cmd_arg_product == 1)glib_self_defined_Source_process(glib_context);if (cmd_arg_product == 2)g_idle_add(idle_func, "idle_func!");if (cmd_arg_product == 3)g_timeout_add(2, timeout_func, glib_context->main_loop);guint watch_id;gint fd = 0;GIOChannel* channel = NULL;if (cmd_arg_product == 4) {channel = g_io_channel_unix_new(1);if(channel) {watch_id = g_io_add_watch(channel, G_IO_IN, io_watch, NULL);g_io_channel_unref(channel);}}g_main_loop_run(glib_context->main_loop);if (cmd_arg_product == 4) {// 退出事件循环g_source_remove (watch_id);// 释放 IO_Channel,内部调用了 g_io_channel_shutdown//g_io_channel_unref (channel);}if (glib_context->main_loop) {g_main_loop_unref(glib_context->main_loop);glib_context->main_loop = NULL;}g_free(glib_context);glib_context = NULL;g_printf("main exit ***\n");return 0;
}

linux - glib使用相关推荐

  1. glib 2.0 arm linux,glib源码安装使用方法

    glib库是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义.相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事 ...

  2. Linux glib命令行解析GOptionEntry使用

    1.安装glib //安装依赖库 sudo apt-get install libffi-dev -y//安装glib # sudo apt-get install libglib2.0-dev -y ...

  3. linux glib,我该如何安装glib?

    问题描述 我想在Ubuntu 11.04中构建Empathy.当我按照构建过程, ./autogen.sh Shell 对我说 libtoolize: copying file `m4/lt~obso ...

  4. Linux glib库hash表GHashTable介绍

    GHashTable 1 简单使用 2 原理分析 3 思考总结 1 简单使用 hash表是一种提供key-value访问的数据结构,通过指定的key值可以快速的访问到与它相关联的value值.hash ...

  5. 几个比较好的IT站和开发库官网

    几个比较好的IT站和开发库官网 1.IT技术.项目类网站 (1)首推CodeProject,一个国外的IT网站,官网地址为:http://www.codeproject.com,这个网站为程序开发者提 ...

  6. glib linux,[转载]linux下glib.h的介绍

    glib库是Linux平台下最常用的C语言函数库,它具有很好的可移植性和实用性.glib是Gtk +库和Gnome的基础.glib可以在多个平台下使用,比如Linux.Unix.Windows等.gl ...

  7. glib linux,如何升级glib呢?

    如何升级glib呢? 我下载了glib2.4.6解压 ./configure make make install 安装完成,但是安装d4x还是提示. checking for GLIB - versi ...

  8. linux安装glib,glib源码安装使用方法

    glib源码下载地址 问题描述:centos 6.5 源码编译qemu  ./configure时出现错误  ERROR: glib-2.22 gthread-2.0 is required to c ...

  9. linux eclipse glib.h,eclipse Glib

    1 安装Eclipse IDE for C/C++ Developers以及glib 2 新建一个C project 3 右击工程,属性->C/C++ General -> Path an ...

最新文章

  1. php中ini set,php ini_set更改php.ini配置功能
  2. 如何生成动态库 .dll 的符号 .lib 文件?
  3. c++ 遍历所有点且距离最短_L3图论第08课 图的遍历
  4. python学习(8)
  5. 前端学习(3171):react-hello-react之reduce
  6. leetcode169. 多数元素——pygo
  7. 超3千万人次观看 李佳琦助力中国青年年货节
  8. forms、forms_toolbar例子理解
  9. mysql集群负载均衡,这些知识你必须拿下
  10. C#仿win10计算器
  11. P2495 [SDOI2011]消耗战(虚树+DP)
  12. shell脚本中 EOF的意思
  13. Winmail反垃圾邮件指南:使用Winmail时,比较有效的过滤垃圾邮件的设置
  14. 机器人java指南_Zmud新手机器人指南
  15. 分享一个07版的office, 有密匙的。
  16. python中的中英文字符统计
  17. 低代码,拯救“疯狂”的程序员
  18. Axure 原型基本介绍
  19. 存储过程-浅尝辄止(游标使用)
  20. spring 之 ObjectPostProcessor

热门文章

  1. go 怎么等待所有的协程完成_优雅地等待子协程执行完毕
  2. 测试开发岗面试系列——大华技术股份有限公司
  3. 计算机的二三事——软件篇
  4. 鼠标事件界面转换 mouseover() 方法
  5. 联想y430p黑苹果之自定义屏幕亮度
  6. 【Shell】 sed/tr替换换行符
  7. 喜马拉雅自研网关架构演进过程
  8. Revit二次开发——引用dynamo中的几何库
  9. 低成本快速开发 LoRa 终端:从 0 到 1
  10. 塑造棋牌游戏文化内涵