GTK+教程编译报错
GTK+ Tutorial Compile error
我正试图在 https://developer.gnome.org/gtk3/stable/ch01s04.html 上完成 GTK+ 教程,但是当我编译代码时出现错误 -pthread: command not found。我正在使用 Ubuntu 17.04。我的编译命令是:
`pkg-config --cflags gtk+-3.0` -o exampleappwin exampleappwin.c `pkg-config --libs gtk+-3.0`
我从 https://git.gnome.org/browse/gtk+/tree/examples/application3 下载文件,所以我知道它们是正确的。提前致谢。
根据下面的评论添加到此。收到错误:
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
然后当我编译 main 时,我收到错误:
/tmp/ccMOUa6f.o: In function `main':
main.c:(.text+0x19): undefined reference to `example_app_new'
collect2: error: ld returned 1 exit status
您提到的示例是更大示例集的一部分,它已准备好与 parent directory/folder.
发布的 Make 或 Meson 一起使用
如果要编译"by hand",那么一定要注意有资源文件需要处理。
The files mentioned on the question above:
[...]$ ls
exampleapp.c exampleapp.h exampleappwin.h Makefile.am meson.build
exampleapp.gresource.xml exampleappwin.c main.c Makefile.example window.ui
[...]$
要仅编译示例,则您应该这样做:
gcc -o exampleapp main.c exampleapp.c exampleappwin.c `pkg-config --cflags --libs gtk+-3.0`
这会编译,但是当您 运行 它时,由于缺少资源会出现警告。
./exampleapp
(exampleapp:4171): Gtk-CRITICAL **: Unable to load resource for composite template for type 'ExampleAppWindow': The resource at '/org/gtk/exampleapp/window.ui' does not exist
(exampleapp:4171): Gtk-CRITICAL **: gtk_widget_class_bind_template_child_full: assertion 'widget_class->priv->template != NULL' failed
(exampleapp:4171): Gtk-CRITICAL **: gtk_widget_init_template: assertion 'template != NULL' failed
现在你应该准备资源(将创建resources.c):
glib-compile-resources exampleapp.gresource.xml --target=resources.c --sourcedir=. --generate-source
然后重新编译(添加上面的resources.c文件):
gcc -o exampleapp main.c exampleapp.c exampleappwin.c resources.c `pkg-config --cflags --libs gtk+-3.0`
警告应该消失并且 window 必须包含标题栏。
您可以在这个问题上找到更多关于编译资源的信息:
GTK/C and GtkBuilder to make a single executable 但请注意,您应该使用项目已有的构建工具,如 Make 或 Meson。
另一种选择是下载示例项目并构建它们。
我正试图在 https://developer.gnome.org/gtk3/stable/ch01s04.html 上完成 GTK+ 教程,但是当我编译代码时出现错误 -pthread: command not found。我正在使用 Ubuntu 17.04。我的编译命令是:
`pkg-config --cflags gtk+-3.0` -o exampleappwin exampleappwin.c `pkg-config --libs gtk+-3.0`
我从 https://git.gnome.org/browse/gtk+/tree/examples/application3 下载文件,所以我知道它们是正确的。提前致谢。
根据下面的评论添加到此。收到错误:
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
然后当我编译 main 时,我收到错误:
/tmp/ccMOUa6f.o: In function `main':
main.c:(.text+0x19): undefined reference to `example_app_new'
collect2: error: ld returned 1 exit status
您提到的示例是更大示例集的一部分,它已准备好与 parent directory/folder.
发布的 Make 或 Meson 一起使用如果要编译"by hand",那么一定要注意有资源文件需要处理。
The files mentioned on the question above:
[...]$ ls
exampleapp.c exampleapp.h exampleappwin.h Makefile.am meson.build
exampleapp.gresource.xml exampleappwin.c main.c Makefile.example window.ui
[...]$
要仅编译示例,则您应该这样做:
gcc -o exampleapp main.c exampleapp.c exampleappwin.c `pkg-config --cflags --libs gtk+-3.0`
这会编译,但是当您 运行 它时,由于缺少资源会出现警告。
./exampleapp
(exampleapp:4171): Gtk-CRITICAL **: Unable to load resource for composite template for type 'ExampleAppWindow': The resource at '/org/gtk/exampleapp/window.ui' does not exist
(exampleapp:4171): Gtk-CRITICAL **: gtk_widget_class_bind_template_child_full: assertion 'widget_class->priv->template != NULL' failed
(exampleapp:4171): Gtk-CRITICAL **: gtk_widget_init_template: assertion 'template != NULL' failed
现在你应该准备资源(将创建resources.c):
glib-compile-resources exampleapp.gresource.xml --target=resources.c --sourcedir=. --generate-source
然后重新编译(添加上面的resources.c文件):
gcc -o exampleapp main.c exampleapp.c exampleappwin.c resources.c `pkg-config --cflags --libs gtk+-3.0`
警告应该消失并且 window 必须包含标题栏。
您可以在这个问题上找到更多关于编译资源的信息: GTK/C and GtkBuilder to make a single executable 但请注意,您应该使用项目已有的构建工具,如 Make 或 Meson。
另一种选择是下载示例项目并构建它们。