我正在尝试用 C 构建一个示例 gtk+

Posted

技术标签:

【中文标题】我正在尝试用 C 构建一个示例 gtk+【英文标题】:I am trying to build a example gtk+ with C 【发布时间】:2021-08-08 03:19:21 【问题描述】:

我正在尝试在 C 上构建我的第一个 gtk+ 程序

我正在使用 simon long 的 C Gui 编程书来学习,我从书中复制了下面的代码

#include <stdio.h>
#include <gtk/gtk.h>

int main (int argc, char *argv[])

    gtk_init (&argc, &argv);
    GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show (win);
    gtk_main ();
    return 0;
    

编译器给我以下错误:

gcc -o bessewellapp bessewellapp.c -Wall `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
In file included from /usr/include/gtk-2.0/gtk/gtkobject.h:37,
                 from /usr/include/gtk-2.0/gtk/gtkwidget.h:36,
                 from /usr/include/gtk-2.0/gtk/gtkcontainer.h:35,
                 from /usr/include/gtk-2.0/gtk/gtkbin.h:35,
                 from /usr/include/gtk-2.0/gtk/gtkwindow.h:36,
                 from /usr/include/gtk-2.0/gtk/gtkdialog.h:35,
                 from /usr/include/gtk-2.0/gtk/gtkaboutdialog.h:32,
                 from /usr/include/gtk-2.0/gtk/gtk.h:33,
                 from bessewellapp.c:2:
/usr/include/gtk-2.0/gtk/gtktypeutils.h:236:1: warning: ‘GTypeDebugFlags’ is deprecated [-Wdeprecated-declarations]
  236 | void            gtk_type_init   (GTypeDebugFlags    debug_flags);
      | ^~~~
In file included from /usr/include/glib-2.0/gobject/gobject.h:24,
                 from /usr/include/glib-2.0/gobject/gbinding.h:29,
                 from /usr/include/glib-2.0/glib-object.h:22,
                 from /usr/include/glib-2.0/gio/gioenums.h:28,
                 from /usr/include/glib-2.0/gio/giotypes.h:28,
                 from /usr/include/glib-2.0/gio/gio.h:26,
                 from /usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30,
                 from /usr/include/gtk-2.0/gdk/gdk.h:32,
                 from /usr/include/gtk-2.0/gtk/gtk.h:32,
                 from bessewellapp.c:2:
/usr/include/glib-2.0/gobject/gtype.h:679:1: note: declared here
  679 | 
      | ^
In file included from /usr/include/gtk-2.0/gtk/gtktoolitem.h:31,
                 from /usr/include/gtk-2.0/gtk/gtktoolbutton.h:30,
                 from /usr/include/gtk-2.0/gtk/gtkmenutoolbutton.h:30,
                 from /usr/include/gtk-2.0/gtk/gtk.h:126,
                 from bessewellapp.c:2:
/usr/include/gtk-2.0/gtk/gtktooltips.h:73:3: warning: ‘GTimeVal’ is deprecated: Use 'GDateTime' instead [-Wdeprecated-declarations]
   73 |   GTimeVal last_popdown;
      |   ^~~~~~~~
In file included from /usr/include/glib-2.0/glib/galloca.h:32,
                 from /usr/include/glib-2.0/glib.h:30,
                 from /usr/include/glib-2.0/gobject/gbinding.h:28,
                 from /usr/include/glib-2.0/glib-object.h:22,
                 from /usr/include/glib-2.0/gio/gioenums.h:28,
                 from /usr/include/glib-2.0/gio/giotypes.h:28,
                 from /usr/include/glib-2.0/gio/gio.h:26,
                 from /usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30,
                 from /usr/include/gtk-2.0/gdk/gdk.h:32,
                 from /usr/include/gtk-2.0/gtk/gtk.h:32,
                 from bessewellapp.c:2:
/usr/include/glib-2.0/glib/gtypes.h:547:8: note: declared here
  547 | struct _GTimeVal

我不知道我的代码有什么问题,知道吗?

【问题讨论】:

摆脱#include &lt;stdio.h&gt;。该包含来自libc,GTK+ 不需要它。 你读过那些消息了吗? 您是否尝试同时使用 GTK2 和 GTK3?如果没有,你不应该通过添加两个标签来混淆读者。 当您尝试编译该代码时,您使用的确切命令是什么? 【参考方案1】:

在您要提供其他信息之前,只需在文件顶部添加以下行即可删除它:

#define GTK_DISABLE_DEPRECATED

应该是这样的:

#define GTK_DISABLE_DEPRECATED

#include <gtk/gtk.h>

int main ( int argc, char *argv[] )

    gtk_init ( &argc, &argv );

    /// ***
    GtkWidget *win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );

    /// ***
    gtk_widget_show ( win );

    /// ***
    gtk_main ();

【讨论】:

以上是关于我正在尝试用 C 构建一个示例 gtk+的主要内容,如果未能解决你的问题,请参考以下文章

将 cairo 与 gtk3 一起使用

将 C 中的 gtk3 与 C++ 中的 gtkmm 结合起来

OSx 单声道 gtk 你好世界。 Gtk.Container 抛出异常

闪烁 - 无法编译诱饵示例

Visual Studio Community 2017 cl 链接器不会链接 GTK3 库?

GTK得到多个范围值分割错误