在我的项目中,我调用gtk_builder_add_from_file
函数来加载xml文件,其中包含之前使用Glade设计的ui对象。所以,我有我的二进制程序和(在同一文件夹中)xml文件。
将所有内容打包到单个可执行文件中的最佳方法是什么?我应该使用自解压脚本吗?还是还有别的东西要一起编译?
感谢所有
答案 0 :(得分:3)
您可以使用GResource
API in GIO。 GResources通过在XML文件中定义您希望随应用程序提供的资产来工作,类似于:
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/example/YourApp">
<file preprocess="xml-stripblanks">your-app.ui</file>
<file>some-image.png</file>
</gresource>
</gresources>
记下prefix
属性,因为它稍后会用到。
一旦您添加了资产,就可以使用GLib附带的glib-compile-resources
二进制文件生成一个C文件,其中包含所有资产,编码为字节数组。生成的代码还将使用各种编译器公开的全局构造函数,以便在加载应用程序之后(并且在调用main
之前)加载资源,或者,如果是共享对象,则一旦库是由链接器加载。 Makefile中glib-compiler-resources
调用的一个示例是:
GLIB_COMPILE_RESOURCES = $(shell $(PKGCONFIG) --variable=glib_compile_resources gio-2.0)
resources = $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=. --generate-dependencies your-app.gresource.xml
your-app-resources.c: your-app.gresource.xml $(resources)
$(GLIB_COMPILE_RESOURCES) your-app.gresource.xml --target=$0 --sourcedir=. --geneate-source
然后您必须将your-app-resources.c
添加到您的构建中。
要访问您的资产,您应该使用各种类中公开的from_resource()
函数;例如,要在GtkBuilder
中加载UI说明,您应该使用gtk_builder_add_from_resource()
。使用的路径是您在GResource XML文件中定义的prefix
和文件名的组合,例如:/com/example/YourApp/your-app.ui
。从resource://
加载时,您还可以使用GFile
URI。
您可以在GResources API reference page找到更多信息。