GTK/C和GtkBuilder制作一个可执行文件 [英] GTK/C and GtkBuilder to make a single executable

查看:337
本文介绍了GTK/C和GtkBuilder制作一个可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my project I call gtk_builder_add_from_file function to load an xml file with the ui objects designed with Glade previously. So, I have my binary program and (in the same folder) the xml file.

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

What is the best way to pack all into a single executable? should I use a self-extracting script? or there is something else to compile all together?

感谢所有人

推荐答案

您可以使用GResourceAPI "GIO中的rel ="nofollow"> . GResources通过在XML文件中定义希望随应用程序一起提供的资产来工作,类似于:

You can use the GResource API available in GIO. GResources work by defining the assets you wish to ship with your application inside an XML file, similar to this:

<?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属性,因为稍后将使用它.

Take note of the prefix attribute, because it will be used later.

添加资产后,将使用GLib随附的glib-compile-resources二进制文件生成一个C文件,该文件包含所有资产,并被编码为字节数组.生成的代码还将使用各种编译器公开的全局构造函数,以便在应用程序加载后(在调用main之前)或在共享库的情况下,通过以下方式加载资源时加载资源:链接器.在Makefile中调用glib-compiler-resources的示例是:

Once you've added your assets, you use the glib-compile-resources binary shipped by GLib to generate a C file that includes all your assets, encoded as byte arrays. The generated code will also use the global constructor functionality exposed by various compilers so that the resources are loaded once your application is loaded (and before main is called), or, in case of a shared object, once the library is loaded by the linker. An example of glib-compiler-resources invocation in a Makefile is:

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添加到构建中.

Then you have to add the your-app-resources.c to your build.

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

In order to access your assets you should use the from_resource() function that is exposed in various classes; for instance, to load a UI description in GtkBuilder, you should use gtk_builder_add_from_resource(). The path used is a combination of the prefix you defined in the GResource XML file and the file name, e.g.: /com/example/YourApp/your-app.ui. You can also use the resource:// URI when loading from a GFile.

您可以在 GResources中找到更多信息. API参考页.

这篇关于GTK/C和GtkBuilder制作一个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆