GTK/C and GtkBuilder to make a single executable

我的梦境 提交于 2019-12-04 05:12:24

问题


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?

Thanks to all


回答1:


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>

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

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

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

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.

You can find more information on the GResources API reference page.



来源:https://stackoverflow.com/questions/28855850/gtk-c-and-gtkbuilder-to-make-a-single-executable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!