![]() |
![]() |
![]() |
GLib参考手册 | ![]() |
---|
编译GLib应用程序编译GLib应用程序 — 如何编译GLib应用程序 |
为了编译GLib应用程序,你需要告诉编译器在哪里找到GLib的头文件及库. pkg-config 工具会帮你.
下面的shell会话展示了 pkg-config 如何工作 (输出可能因不同的系统有所不同):
$ pkg-config --cflags glib-2.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include $ pkg-config --libs glib-2.0 -L/usr/lib -lm -lglib-2.0
如果你的应用程序使用线程(threads)或者 GObject 特性, 则必须用以下的 pkg-config 输出来编译及链接:
$ pkg-config --cflags --libs gthread-2.0 $ pkg-config --cflags --libs gobject-2.0
如果你的应用程序使用模块(modules), 则必须用以下的 pkg-config 输出来编译及链接:
$ pkg-config --cflags --libs gmodule-no-export-2.0 $ pkg-config --cflags --libs gmodule-2.0
两者的不同之处在于gmodule-2.0 为连接添加了--export-dynamic
标志, 但它并不经常用到.
最简单的编译方式是使用shell的"反引号"特性. 如果你将命令放入反引号里 (不是单引号), 那么它的输出会在命令行执行前 取代命令.所以编译一个GLib的Hello,World,你输入以下命令:
$ cc `pkg-config --cflags --libs glib-2.0` hello.c -o hello
如果你确定你的程序不使用任何过时的函数,你可以为预处理器定义
G_DISABLE_DEPRECATED 符号,使用命令选项 -DG_DISABLE_DEPRECATED=1
.
使用GLib的推荐方式是只包含最顶层的头文件 glib.h
,
glib-object.h
, gio.h
.
从2.17开始,当直接包含单独的头文件时GLib会强制生成一个错误.
为了帮助大家过渡,执行器在默认的Glib头文件中未打开(但GObject和GIO是打开的)
为了打开它,使用命令行 -DG_DISABLE_DEPRECATED
来定义预处理器符号 G_DISABLE_SINGLE_INCLUDES
即可.
glib/gprintf.h
and glib/gstdio.h
(we don't want to pull in all of stdio).
Starting with 2.17, GLib enforces this by generating an error
when individual headers are directly included. To help with the
transition, the enforcement is not turned on by default for GLib
headers (it is turned on for GObject and GIO).
To turn it on, define the preprocessor symbol G_DISABLE_SINGLE_INCLUDES
by using the command line option -DG_DISABLE_SINGLE_INCLUDES
.