GTK/C 和 GtkBuilder 制作单个可执行文件

Rob*_*Man 3 gtkbuilder gtk3

在我的项目中,我调用gtk_builder_add_from_file函数来加载带有之前用 Glade 设计的 ui 对象的 xml 文件。所以,我有我的二进制程序和(在同一文件夹中)xml 文件。

将所有内容打包到单个可执行文件中的最佳方法是什么?我应该使用自解压脚本吗?或者还有其他东西可以一起编译?

谢谢大家

eba*_*ssi 5

您可以使用GIO 中GResource提供的 API 。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>
Run Code Online (Sandbox Code Playgroud)

记下该prefix属性,因为稍后会用到它。

添加资产后,您可以使用glib-compile-resourcesGLib 提供的二进制文件生成一个 C 文件,其中包含所有资产(编码为字节数组)。生成的代码还将使用各种编译器公开的全局构造函数功能,以便在加载应用程序(并且在main调用之前)后加载资源,或者,如果是共享对象,则在链接器加载库后加载资源。glib-compiler-resourcesMakefile 中的调用示例如下:

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
Run Code Online (Sandbox Code Playgroud)

然后你必须将其添加your-app-resources.c到你的构建中。

为了访问您的资产,您应该使用from_resource()各种类中公开的函数;例如,要在 中加载 UI 描述GtkBuilder,您应该使用gtk_builder_add_from_resource(). 使用的路径是prefix您在 GResource XML 文件中定义的路径和文件名的组合,例如:/com/example/YourApp/your-app.ui。您还可以resource://在从GFile.

您可以在GResources API 参考页面上找到更多信息。