GTK +教程编译错误

时间:2017-06-16 02:20:38

标签: compiler-errors gtk gtk3

我正在尝试在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        

1 个答案:

答案 0 :(得分:0)

您提到的示例是一组更大的示例的一部分,它准备与从父目录/文件夹发出的Make或Meson一起使用。

如果您想“手动”编译,那么您必须注意到有资源文件需要处理。

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`

警告应该消失,窗口必须包含标题栏。

您可以在此问题上找到有关编译资源的更多信息: GTK/C and GtkBuilder to make a single executable但请注意,您应该使用像项目已有的Make或Meson等构建工具。

另一种选择是下载示例项目并构建它们。