2017-06-16 157 views
0

我想完成在https://developer.gnome.org/gtk3/stable/ch01s04.html上的GTK +教程,但是當我編譯代碼時,得到錯誤-pthread:command not found。我正在使用Ubuntu 17.04。我的編譯命令是:GTK +教程編譯錯誤

`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 

後來,當我編譯主,我收到錯誤:

/tmp/ccMOUa6f.o: In function `main': 

    main.c:(.text+0x19): undefined reference to `example_app_new' 

    collect2: error: ld returned 1 exit status   
+1

你忘了指定編譯器。 – andlabs

+0

例子:'gcc -o exampleappwin exampleappwin.c \'pkg-config --libs gtk + -3.0 \''(其中gcc是編譯器as andlabs提到的) –

+0

哇!我無法相信我做到了!我總是把它放在評論中,以便剪切和粘貼。好。我收到一個錯誤:'/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o:函數'_start': (.text + 0x20):對'main'的未定義引用 collect2:錯誤:ld返回1退出狀態。當我嘗試編譯main時,收到一個錯誤:'/tmp/ccMOUa6f.o:在main函數中: main .c :(。text + 0x19):未定義的引用'example_app_new' collect2:錯誤:ld返回1退出狀態' – Guest123ABC

回答

0

你提到的這個例子是一個更大的集的例子的一部分,它的準備與製作使用或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` 

這將編譯,但是當你運行它,警告會出現由於missings資源。

./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等構建工具作爲項目已有的工具。

另一種選擇是下載示例項目並將它們全部構建。

+0

好的。我做到了這一切,似乎一切工作到目前爲止。非常感謝你。對不起,第一篇文章。我仍然無法相信我做到了。爲頁面添加書籤。再次感謝。對此,我真的非常感激。我確實建立了他們的記錄。使用它們來學習GTK。 – Guest123ABC

+0

@ Guest123ABC很高興它的工作,並隨時問你學習GTK的問題。如果這個答案解決了你的問題,隨時接受它是正確的。祝你好運 –

+0

我試圖提高你的答案,但是我的聲望更低。它表示它計數上升,但不會顯示,直到獲得更高的聲譽。 :) – Guest123ABC