You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为glib-compile-resources配置autotools构建规则

第一步:修改configure.ac 配置

你需要先让构建系统识别glib-compile-resources工具,在configure.ac里添加以下内容,放在AC_OUTPUT之前即可:

# 检查glib2.0依赖,确保glib-compile-resources存在
PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.50])
# 单独查找glib-compile-resources可执行文件路径
AC_PATH_PROG([GLIB_COMPILE_RESOURCES], [glib-compile-resources], [no])
if test "x$GLIB_COMPILE_RESOURCES" = "xno"; then
  AC_MSG_ERROR([glib-compile-resources not found, please install glib2 development packages])
fi

不需要修改autogen.h,该文件是构建过程自动生成的,无需手动调整。

第二步:修改Makefile.am配置

通常资源描述文件(*.gresource.xml)和对应的界面、图片等资源都会放在data目录下,所以建议在data/Makefile.am里添加规则,如果你非要放在顶层Makefile.am也可以参考下面的规则调整路径即可。你现有的顶层Makefile.am已经把data加入了SUBDIRS列表,不需要额外修改。

场景1:把资源编译成C代码嵌入程序

假设你有个资源描述文件叫app.gresource.xml,需要生成嵌入程序用的C源文件,data/Makefile.am添加内容如下:

# 定义资源描述文件路径
gresource_xml = app.gresource.xml
# 列出xml里引用的所有资源文件,比如ui文件、图片、样式表等
resource_files = \
  main.ui \
  icon.png \
  style.css

# 标记构建前需要先生成的源文件
BUILT_SOURCES = app-resources.c app-resources.h

# 生成C源文件的规则
app-resources.c: $(gresource_xml) $(resource_files)
	$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) \
		--target=$@ \
		--sourcedir=$(srcdir) \
		--generate-source \
		$<

# 生成头文件的规则
app-resources.h: $(gresource_xml) $(resource_files)
	$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) \
		--target=$@ \
		--sourcedir=$(srcdir) \
		--generate-header \
		$<

# 把资源文件加入发布包,避免make dist时遗漏
EXTRA_DIST += $(gresource_xml) $(resource_files)

# 编译生成的文件要在make clean时删除
CLEANFILES = $(BUILT_SOURCES)

如果生成的C文件要在src目录的代码里引用,记得在src/Makefile.am里把头文件路径加入对应目标的_CFLAGS,把app-resources.c加入对应目标的_SOURCES即可。

场景2:生成独立的二进制资源文件安装到系统

如果你不需要嵌入源码,而是要直接编译成二进制.gresource文件安装到系统,data/Makefile.am添加内容如下:

gresource_xml = app.gresource.xml
resource_files = \
  main.ui \
  icon.png \
  style.css

# 生成的二进制资源文件
gresource_out = app.gresource

# 资源安装目录,根据你的应用名调整即可
resourcedir = $(datadir)/gjiten/
resource_DATA = $(gresource_out)

# 编译二进制资源的规则
$(gresource_out): $(gresource_xml) $(resource_files)
	$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) \
		--target=$@ \
		--sourcedir=$(srcdir) \
		$<

EXTRA_DIST += $(gresource_xml) $(resource_files)
CLEANFILES = $(gresource_out)

内容的提问来源于stack exchange,提问作者DarkTrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 19:45:03