ToshioCP / Gtk4-tutorial

GTK 4 tutorial for beginners
https://toshiocp.github.io/Gtk4-tutorial/
548 stars 50 forks source link

gresource problem chapter 9 #32

Closed feinap closed 1 year ago

feinap commented 1 year ago

In the end of section 9 you are creating a resources.c file with gresource but in tfe3_r.c, you are not using it.

Then a C source file resources.c is generated. Modify tfe3.c and save it as tfe3_r.c.

#include "resources.c"
... ... ...
... ... ...
build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe3.ui");
... ... ...
... ... ...

Then, compile and run it. The window appears and it is the same as the screenshot at the beginning of this page.

U still pointing previously created ui file. What is the point here? And after getting resource.c and compiling it with tfe3_r.c, im getting

/home/dnf/codeblockprojs/tfe3/resources.c|147|multiple definition of `tfe3_get_resource'; obj/Debug/resources.o:/home/dnf/codeblockprojs/tfe3/resources.c:147: first defined here|

The code is in resources.c:

.
.
static GStaticResource static_resource = { tfe3_resource_data.data, sizeof (tfe3_resource_data.data) - 1 /* nul terminator */, NULL, NULL, NULL };

G_MODULE_EXPORT
GResource *tfe3_get_resource (void);
GResource *tfe3_get_resource (void)
{
  return g_static_resource_get_resource (&static_resource);
}
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
.
.

if i comment out second definition, programs builds and run but i didnt understand what is the problem here.

ToshioCP commented 1 year ago

Thank you for posting the issue, feinap.

In the tutorial, I wanted to show that a gresource (resources.c) can be used instead of a file or string. And using Gresource is the best way.

The source files are in src/tfe. So, I'd like you to clone the repository and use the files in this directory.

$ glib-compile-resources tfe3.gresource.xml --target=resources.c --generate-source
$ bash comp tfe3_r
$ ./a.out taketori.txt

Then a window appears.

The file comp is a shell file.

$ cat comp
gcc `pkg-config --cflags gtk4` $1.c `pkg-config --libs gtk4`

It runs gcc with the above options.

Actually, I still don't get what you did. But, the resources.c you wrote was probably OK. So, a problem may be in tfe3_r.c or compilation.

Try the way above and check your tfe3_r.c with src/tfe/tfe3_r.c. Further question is also welcome.

feinap commented 1 year ago

I mean, shouldnt we write like this to use gresource:

 #include "resources.c"
 ... ... ...
 ... ... ...
 //build = gtk_builder_new_from_resource ("tfe3.ui");
build = gtk_builder_new_from_resource ("resources.c");
 ... ... ...
 ... ... ...

When i am using build = gtk_builder_new_from_resource ("tfe3.ui");, we are still dependent to .ui file, yes it works very well. But we are creating a .c file from .ui, but we didnt use it in the code, just inculuded "resources.c" but actually we didnt need it at this point.

ToshioCP commented 1 year ago

The resource compiler creates a resource from tfe3.ui. So the content of the resource is the same as tfe3.ui. And the resource is registered in a certain area of the executable file a.out. The resource can be accessed with the resource path (not file system path).

In the example tfe3_r.c, /com/github/ToshioCP/tfe3/tfe3.ui is the resource path.

Therefore,

build = gtk_builder_new_from_resource ("resources.c");

doesn't work.

For more information, see Gio documentation.

https://docs.gtk.org/gio/struct.Resource.html

ToshioCP commented 1 year ago

It seems that no further questions will be posted. The issue is now closed.