GTK+ 3.0 编译错误:不能包含 errno.h

Posted

技术标签:

【中文标题】GTK+ 3.0 编译错误:不能包含 errno.h【英文标题】:GTK+ 3.0 compilation error: cannot include errno.h 【发布时间】:2020-10-08 20:30:21 【问题描述】:

我无法在我的 GTK+ 应用程序中使用 #include <errno.h> 标头。 GTK+ 2 和 3 都试过了,还是报错。

如果我不包括errno.h,它会说它不包括在内。但是,如果我这样做 ld 会失败,说没有包含 GTK 库中的函数。

gtk3 的编译日志(无 errno)

gcc `pkg-config --cflags --libs gtk+-3.0` -o winopa_launcher winopa_launcher.cwinopa_launcher.c: In function ‘on_set_btn_clicked’:
winopa_launcher.c:21:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   21 |   entry_value = gtk_entry_get_text(entry);
      |               ^
winopa_launcher.c:24:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   24 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "\"%s\" is not a valid integer", entry_value);
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c:24:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   24 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "\"%s\" is not a valid integer", entry_value);
      |            ^
winopa_launcher.c:26:24: warning: passing argument 1 of ‘gtk_widget_destroy’ from incompatible pointer type [-Wincompatible-pointer-types]
   26 |     gtk_widget_destroy(dialog);
      |                        ^~~~~~
      |                        |
      |                        GtkMessageDialog * aka struct _GtkMessageDialog *
In file included from /usr/include/gtk-3.0/gtk/gtkapplication.h:27,
                 from /usr/include/gtk-3.0/gtk/gtkwindow.h:33,
                 from /usr/include/gtk-3.0/gtk/gtkdialog.h:32,
                 from /usr/include/gtk-3.0/gtk/gtkaboutdialog.h:30,
                 from /usr/include/gtk-3.0/gtk/gtk.h:31,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkwidget.h:619:50: note: expected ‘GtkWidget *’ aka ‘struct _GtkWidget *’ but argument is of type ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’
  619 | void    gtk_widget_destroy    (GtkWidget        *widget);
      |                                ~~~~~~~~~~~~~~~~~~^~~~~~
winopa_launcher.c:32:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   32 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c:32:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   32 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |            ^
winopa_launcher.c:34:24: warning: passing argument 1 of ‘gtk_widget_destroy’ from incompatible pointer type [-Wincompatible-pointer-types]
   34 |     gtk_widget_destroy(dialog);
      |                        ^~~~~~
      |                        |
      |                        GtkMessageDialog * aka struct _GtkMessageDialog *
In file included from /usr/include/gtk-3.0/gtk/gtkapplication.h:27,
                 from /usr/include/gtk-3.0/gtk/gtkwindow.h:33,
                 from /usr/include/gtk-3.0/gtk/gtkdialog.h:32,
                 from /usr/include/gtk-3.0/gtk/gtkaboutdialog.h:30,
                 from /usr/include/gtk-3.0/gtk/gtk.h:31,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkwidget.h:619:50: note: expected ‘GtkWidget *’ aka ‘struct _GtkWidget *’ but argument is of type ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’
  619 | void    gtk_widget_destroy    (GtkWidget        *widget);
      |                                ~~~~~~~~~~~~~~~~~~^~~~~~
winopa_launcher.c:41:156: error: ‘g_errno’ undeclared (first use in this function); did you mean ‘g_error’?
   41 | GE_ERROR, GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", g_errno);
      |                                                                       ^~~~~~~
      |                                                                       g_error
winopa_launcher.c:41:156: note: each undeclared identifier is reported only once for each function it appears in
winopa_launcher.c:41:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   41 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", g_errno);
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c:43:24: warning: passing argument 1 of ‘gtk_widget_destroy’ from incompatible pointer type [-Wincompatible-pointer-types]
   43 |     gtk_widget_destroy(dialog);
      |                        ^~~~~~
      |                        |
      |                        GtkMessageDialog * aka struct _GtkMessageDialog *
In file included from /usr/include/gtk-3.0/gtk/gtkapplication.h:27,
                 from /usr/include/gtk-3.0/gtk/gtkwindow.h:33,
                 from /usr/include/gtk-3.0/gtk/gtkdialog.h:32,
                 from /usr/include/gtk-3.0/gtk/gtkaboutdialog.h:30,
                 from /usr/include/gtk-3.0/gtk/gtk.h:31,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkwidget.h:619:50: note: expected ‘GtkWidget *’ aka ‘struct _GtkWidget *’ but argument is of type ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’
  619 | void    gtk_widget_destroy    (GtkWidget        *widget);
      |                                ~~~~~~~~~~~~~~~~~~^~~~~~
winopa_launcher.c: In function ‘main’:
winopa_launcher.c:59:3: warning: ‘gtk_vbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   59 |   vbox = gtk_vbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:286,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkvbox.h:61:13: note: declared here
   61 | GtkWidget * gtk_vbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~
winopa_launcher.c:64:3: warning: ‘gtk_hbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   64 |   hbox = gtk_hbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:262,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkhbox.h:63:13: note: declared here
   63 | GtkWidget * gtk_hbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~
mattia@mattia-ThinkPad-E595:~/lab$ gtk3-gcc -o winopa_launcher winopa_launcher.c
winopa_launcher.c: In function ‘on_set_btn_clicked’:
winopa_launcher.c:25:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   25 |     dialog = gtk_message_dialog_new(GTK_WINDOW(widget), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "\"%s\" is not a valid integer", entry_value);
      |            ^
winopa_launcher.c:33:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   33 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c:33:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   33 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |            ^
winopa_launcher.c:39:12: warning: passing argument 1 of ‘snprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
   39 |   snprintf(hexvalue, 10, "0x%x", 0xffffffff * value / 100);
      |            ^~~~~~~~
      |            |
      |            char **
In file included from /usr/include/pango-1.0/pango/pango-utils.h:25,
                 from /usr/include/pango-1.0/pango/pango.h:47,
                 from /usr/include/gtk-3.0/gdk/gdktypes.h:35,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:30,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from winopa_launcher.c:1:
/usr/include/stdio.h:354:39: note: expected ‘char * restrict’ but argument is of type ‘char **’
  354 | extern int snprintf (char *__restrict __s, size_t __maxlen,
      |                      ~~~~~~~~~~~~~~~~~^~~
winopa_launcher.c:42:156: error: ‘errno’ undeclared (first use in this function)
   42 |  GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |                                                              ^~~~~

winopa_launcher.c:2:1: note: ‘errno’ is defined in header ‘<errno.h>’; did you forget to ‘#include <errno.h>’?
    1 | #include <gtk/gtk.h>
  +++ |+#include <errno.h>
    2 | 
winopa_launcher.c:42:156: note: each undeclared identifier is reported only once for each function it appears in
   42 |  GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |                                                              ^~~~~

winopa_launcher.c:42:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   42 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c: In function ‘main’:
winopa_launcher.c:60:3: warning: ‘gtk_vbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   60 |   vbox = gtk_vbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:286,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkvbox.h:61:13: note: declared here
   61 | GtkWidget * gtk_vbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~
winopa_launcher.c:65:3: warning: ‘gtk_hbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   65 |   hbox = gtk_hbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:262,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkhbox.h:63:13: note: declared here
   63 | GtkWidget * gtk_hbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~
mattia@mattia-ThinkPad-E595:~/lab$ gtk3-gcc -o winopa_launcher winopa_launcher.c
winopa_launcher.c: In function ‘on_set_btn_clicked’:
winopa_launcher.c:25:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   25 |     dialog = gtk_message_dialog_new(GTK_WINDOW(widget), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "\"%s\" is not a valid integer", entry_value);
      |            ^
winopa_launcher.c:33:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   33 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c:33:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   33 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |            ^
winopa_launcher.c:39:12: warning: passing argument 1 of ‘snprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
   39 |   snprintf(hexvalue, 10, "0x%x", 0xffffffff * value / 100);
      |            ^~~~~~~~
      |            |
      |            char **
In file included from /usr/include/pango-1.0/pango/pango-utils.h:25,
                 from /usr/include/pango-1.0/pango/pango.h:47,
                 from /usr/include/gtk-3.0/gdk/gdktypes.h:35,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:30,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from winopa_launcher.c:1:
/usr/include/stdio.h:354:39: note: expected ‘char * restrict’ but argument is of type ‘char **’
  354 | extern int snprintf (char *__restrict __s, size_t __maxlen,
      |                      ~~~~~~~~~~~~~~~~~^~~
winopa_launcher.c:42:156: error: ‘errno’ undeclared (first use in this function)
   42 |  GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |                                                              ^~~~~

winopa_launcher.c:2:1: note: ‘errno’ is defined in header ‘<errno.h>’; did you forget to ‘#include <errno.h>’?
    1 | #include <gtk/gtk.h>
  +++ |+#include <errno.h>
    2 | 
winopa_launcher.c:42:156: note: each undeclared identifier is reported only once for each function it appears in
   42 |  GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |                                                              ^~~~~

winopa_launcher.c:42:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   42 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c: In function ‘main’:
winopa_launcher.c:60:3: warning: ‘gtk_vbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   60 |   vbox = gtk_vbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:286,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkvbox.h:61:13: note: declared here
   61 | GtkWidget * gtk_vbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~
winopa_launcher.c:65:3: warning: ‘gtk_hbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   65 |   hbox = gtk_hbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:262,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkhbox.h:63:13: note: declared here
   63 | GtkWidget * gtk_hbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~

gtk3 的编译日志(带 errno)

$ gcc `pkg-config --cflags --libs gtk+-3.0` -o winopa_launcher winopa_launcher.c
winopa_launcher.c: In function ‘on_set_btn_clicked’:
winopa_launcher.c:26:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   26 |     dialog = gtk_message_dialog_new(GTK_WINDOW(widget), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "\"%s\" is not a valid integer", entry_value);
      |            ^
winopa_launcher.c:34:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   34 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c:34:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   34 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Expected a percentage between 5 and 100");
      |            ^
winopa_launcher.c:40:12: warning: passing argument 1 of ‘snprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
   40 |   snprintf(hexvalue, 10, "0x%x", 0xffffffff * value / 100);
      |            ^~~~~~~~
      |            |
      |            char **
In file included from /usr/include/pango-1.0/pango/pango-utils.h:25,
                 from /usr/include/pango-1.0/pango/pango.h:47,
                 from /usr/include/gtk-3.0/gdk/gdktypes.h:35,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:30,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from winopa_launcher.c:1:
/usr/include/stdio.h:354:39: note: expected ‘char * restrict’ but argument is of type ‘char **’
  354 | extern int snprintf (char *__restrict __s, size_t __maxlen,
      |                      ~~~~~~~~~~~~~~~~~^~~
winopa_launcher.c:43:37: warning: passing argument 1 of ‘gtk_message_dialog_new’ from incompatible pointer type [-Wincompatible-pointer-types]
   43 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |                                     ^~~~~~
      |                                     |
      |                                     GtkWidget * aka struct _GtkWidget *
In file included from /usr/include/gtk-3.0/gtk/gtk.h:150,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/gtkmessagedialog.h:99:12: note: expected ‘GtkWindow *’ aka ‘struct _GtkWindow *’ but argument is of type ‘GtkWidget *’ aka ‘struct _GtkWidget *’
   99 | GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
      |            ^~~~~~~~~~~~~~~~~~~~~~
winopa_launcher.c:43:12: warning: assignment to ‘GtkMessageDialog *’ aka ‘struct _GtkMessageDialog *’ from incompatible pointer type ‘GtkWidget *’ aka ‘struct _GtkWidget *’ [-Wincompatible-pointer-types]
   43 |     dialog = gtk_message_dialog_new(widget, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Could not execute command. [Errno %d]", errno);
      |            ^
winopa_launcher.c: In function ‘main’:
winopa_launcher.c:61:3: warning: ‘gtk_vbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   61 |   vbox = gtk_vbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:286,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkvbox.h:61:13: note: declared here
   61 | GtkWidget * gtk_vbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~
winopa_launcher.c:66:3: warning: ‘gtk_hbox_new’ is deprecated: Use 'gtk_box_new' instead [-Wdeprecated-declarations]
   66 |   hbox = gtk_hbox_new(TRUE, 5);
      |   ^~~~
In file included from /usr/include/gtk-3.0/gtk/gtk.h:262,
                 from winopa_launcher.c:1:
/usr/include/gtk-3.0/gtk/deprecated/gtkhbox.h:63:13: note: declared here
   63 | GtkWidget * gtk_hbox_new      (gboolean homogeneous,
      |             ^~~~~~~~~~~~
/usr/bin/ld: /tmp/ccrQ0vyC.o: in function `destroy':
winopa_launcher.c:(.text+0x21): undefined reference to `g_print'
/usr/bin/ld: winopa_launcher.c:(.text+0x26): undefined reference to `gtk_main_quit'
/usr/bin/ld: /tmp/ccrQ0vyC.o: in function `on_set_btn_clicked':
winopa_launcher.c:(.text+0x6c): undefined reference to `gtk_entry_get_text'
/usr/bin/ld: winopa_launcher.c:(.text+0xad): undefined reference to `g_print'
/usr/bin/ld: winopa_launcher.c:(.text+0xb2): undefined reference to `gtk_window_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0xc7): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0xf1): undefined reference to `gtk_message_dialog_new'
/usr/bin/ld: winopa_launcher.c:(.text+0xfa): undefined reference to `gtk_dialog_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x10c): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x114): undefined reference to `gtk_dialog_run'
/usr/bin/ld: winopa_launcher.c:(.text+0x119): undefined reference to `gtk_widget_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x12b): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x133): undefined reference to `gtk_widget_destroy'
/usr/bin/ld: winopa_launcher.c:(.text+0x162): undefined reference to `g_print'
/usr/bin/ld: winopa_launcher.c:(.text+0x18c): undefined reference to `gtk_message_dialog_new'
/usr/bin/ld: winopa_launcher.c:(.text+0x195): undefined reference to `gtk_dialog_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x1a7): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x1af): undefined reference to `gtk_dialog_run'
/usr/bin/ld: winopa_launcher.c:(.text+0x1b4): undefined reference to `gtk_widget_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x1c6): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x1ce): undefined reference to `gtk_widget_destroy'
/usr/bin/ld: winopa_launcher.c:(.text+0x291): undefined reference to `gtk_message_dialog_new'
/usr/bin/ld: winopa_launcher.c:(.text+0x29a): undefined reference to `gtk_dialog_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x2ac): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x2b4): undefined reference to `gtk_dialog_run'
/usr/bin/ld: winopa_launcher.c:(.text+0x2b9): undefined reference to `gtk_widget_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x2cb): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x2d3): undefined reference to `gtk_widget_destroy'
/usr/bin/ld: /tmp/ccrQ0vyC.o: in function `main':
winopa_launcher.c:(.text+0x316): undefined reference to `gtk_init'
/usr/bin/ld: winopa_launcher.c:(.text+0x320): undefined reference to `gtk_window_new'
/usr/bin/ld: winopa_launcher.c:(.text+0x34f): undefined reference to `g_signal_connect_data'
/usr/bin/ld: winopa_launcher.c:(.text+0x362): undefined reference to `gtk_window_set_title'
/usr/bin/ld: winopa_launcher.c:(.text+0x371): undefined reference to `gtk_vbox_new'
/usr/bin/ld: winopa_launcher.c:(.text+0x381): undefined reference to `gtk_label_new'
/usr/bin/ld: winopa_launcher.c:(.text+0x38a): undefined reference to `gtk_box_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x39c): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x3bb): undefined reference to `gtk_box_pack_end'
/usr/bin/ld: winopa_launcher.c:(.text+0x3ca): undefined reference to `gtk_hbox_new'
/usr/bin/ld: winopa_launcher.c:(.text+0x3d3): undefined reference to `gtk_entry_new'
/usr/bin/ld: winopa_launcher.c:(.text+0x3dc): undefined reference to `gtk_entry_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x3ee): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x3fd): undefined reference to `gtk_entry_set_text'
/usr/bin/ld: winopa_launcher.c:(.text+0x402): undefined reference to `gtk_box_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x414): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x433): undefined reference to `gtk_box_pack_end'
/usr/bin/ld: winopa_launcher.c:(.text+0x43f): undefined reference to `gtk_button_new_with_label'
/usr/bin/ld: winopa_launcher.c:(.text+0x448): undefined reference to `gtk_box_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x45a): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x479): undefined reference to `gtk_box_pack_end'
/usr/bin/ld: winopa_launcher.c:(.text+0x47e): undefined reference to `gtk_box_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x490): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x4af): undefined reference to `gtk_box_pack_end'
/usr/bin/ld: winopa_launcher.c:(.text+0x4b4): undefined reference to `gtk_container_get_type'
/usr/bin/ld: winopa_launcher.c:(.text+0x4c6): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: winopa_launcher.c:(.text+0x4d8): undefined reference to `gtk_container_add'
/usr/bin/ld: winopa_launcher.c:(.text+0x4e4): undefined reference to `gtk_widget_show_all'
/usr/bin/ld: winopa_launcher.c:(.text+0x4e9): undefined reference to `gtk_main'
collect2: error: ld returned 1 exit status

gtk2 也是如此。

我在 Internet 上找不到任何解决方案。我正在运行带有 gcc 9.3.0 的 Ubuntu 20.04,以及新安装的主要 Ubuntu apt 存储库中的 GTK。

编辑:注释掉包含errno的代码后,我发现errno.h没有问题。问题出在 gtk 本身。但是仍然收到很多可怕的警告,而且我懒得插入所有适当的类型转换。

【问题讨论】:

【参考方案1】:

已解决!

问题在于命令行参数。我本末倒置。标志总是跟随所有其他参数。

移动

$ gcc `pkg-config --libs --cflags gtk+-3.0` -o winopa_launcher winopa_launcher.c

$ gcc -o winopa_launcher winopa_launcher.c `pkg-config --libs --cflags gtk+-3.0`

可以解决问题。

【讨论】:

以上是关于GTK+ 3.0 编译错误:不能包含 errno.h的主要内容,如果未能解决你的问题,请参考以下文章

Package gtk+-3.0 was not found in the pkg-config search path

GTK+ 3.0:如何将 Gtk.TreeStore 与自定义模型项一起使用?

扩展器内带有树视图的 Gtk 3.0 窗口

Flutter Doctor 显示在 Doctor 上运行的 CentOS Linux 7 的 GTK 3.0 存在问题

undefined reference to `gdk_monitor_get_scale_factor/gtk_widget_get_scale_factor‘

linux下使用C语言进行GTK桌面程序开发