【问题标题】:Compiling and running GTK+ application on Windows 7在 Windows 7 上编译和运行 GTK+ 应用程序
【发布时间】:2026-02-14 16:35:01
【问题描述】:

系统:Windows7,32位,GTK 2.24.10,mingw
我正在尝试编写基于 helloworld.c 类型 GTK 的基本应用程序。但是,它没有运行。
这些是我遵循的步骤。

  1. 安装 MinGW。
  2. 在一个包中下载 GTK+。
  3. 解压 C:\gtk 文件夹中的内容。
  4. 打开cmd进入C:\gtk\bin目录运行pkg-config --cflags --libs gtk+-win32-2.0
  5. 它会打印编译标志列表和库以链接您的 项目到。 复制它们并创建一个浴缸文件,如下所示。 设置 VAR=FLAGS 启动命令 其中 VAR = GTK,FLAGS = 上一个命令 (pkg-config) 的输出。 当你想编译文件时使用命令:gcc foo.c %VAR%

D:\gtk>gcc -o 项目 helloworld.c %GTK%
gcc: %GTK%: 没有这样的文件或目录 helloworld.c:1:21: 错误: gtk/gtk.h: 没有这样的文件或目录 helloworld.c:在函数'main'中: helloworld.c:5: error: 'GtkWidget' undeclared (首先在这个函数中使用) helloworld.c:5: 错误:(每个未声明的标识符只报告一次 helloworld.c:5:错误:对于它出现的每个函数。) helloworld.c:5: error: 'window' undeclared (第一次在这个函数中使用) helloworld.c:9: error: 'GTK_WINDOW_TOPLEVEL' undeclared (第一次在这个函数中使用)

D:\gtk>gcc -Wall -g helloworld.c -o helloworld pkg-config --cflags gtk+-2.0 pkg-config --libs gtk+-2.0
gcc: pkg-config: No such file or directory
gcc: gtk+-2.0: No such file or directory
gcc: pkg-config: No such file or directory
gcc: gtk+-2.0: No such file or directory
cc1.exe: error: unrecognized command line option "-fcflags"
cc1.exe: error: unrecognized command line option "-flibs"

D:\gtk 中的批处理文件

set GTK=-mms-bitfields -IC:/gtk/include/gtk-2.0 -IC:/gtk/lib/gtk-2.0/include -IC:/gtk/include/atk-1.0 -IC:/gtk/include/cairo -IC:/gtk/include/gdk-pixbuf-2.0 -IC:/gtk/include/pango-1.0 -IC:/gtk/include/glib-2.0 -IC:/gtk/lib/glib-2.0/include -IC:/gtk/include -IC:/gtk/include/freetype2 -IC:/gtk/include/libpng14  -LC:/gtk/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl
start cmd

helloworld.c

#include <gtk/gtk.h>

int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show  (window);

    gtk_main ();

    return 0;
}

参考:Installing gtk and compiling using gcc under windows?

【问题讨论】:

    标签: c++ c windows gcc gtk


    【解决方案1】:

    您可以尝试以下手动步骤:

    1) 在命令提示符处运行 pkg-config 命令以获取包含标志:

    c:\dev\gtk224\bin\pkg-config.exe --cflags gtk+-2.0

    这是我的输出:

    -mms-bitfields -Ic:/dev/gtk224/include/gtk-2.0 -Ic:/dev/gtk224/lib/gtk-2.0/include -Ic:/dev/gtk224/include/atk-1.0 -Ic:/dev/gtk224/include/cairo -Ic:/dev/gtk224/include/gdk-pixbuf-2.0 -Ic:/dev/gtk224/include/pango-1.0 -Ic:/dev/gtk224/include/glib-2.0 -Ic:/dev/gtk224/lib/glib-2.0/include -Ic:/dev/gtk224/include -Ic:/dev/gtk224/include/freetype2 -Ic:/dev/gtk224/include/libpng14
    

    2) 将 (1) 的输出设置为变量 GTK_INCLUDES:

    C:\dev\1_repo\gtk_scratch>set GTK_INCLUDES=-mms-bitfields -Ic:/dev/gtk224/include/gtk-2.0 -Ic:/dev/gtk224/lib/gtk-2.0/include -Ic:/dev/gtk224/include/atk-1.0 -Ic:/dev/gtk224/include/cairo -Ic:/dev/gtk224/include/gdk-pixbuf-2.0 -Ic:/dev/gtk224/include/pango-1.0 -Ic:/dev/gtk224/include/glib-2.0 -Ic:/dev/gtk224/lib/glib-2.0/include -Ic:/dev/gtk224/include -Ic:/dev/gtk224/include/freetype2 -Ic:/dev/gtk224/include/libpng14
    

    (确保使用步骤 (1) 中的输出)

    3) 对库标志执行与第 1 步相同的操作:

    c:\dev\gtk224\bin\pkg-config.exe --libs gtk+-2.0

    这是我的输出:

    -Lc:/dev/gtk224/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl
    

    4) 将 (3) 的输出设置为变量 GTK_LIBS

    C:\dev\1_repo\gtk_scratch>set GTK_LIBS=-Lc:/dev/gtk224/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl
    

    (确保使用步骤 (3) 中的输出)

    5) 确保 gtk+ 和 MinGW 在您的路径上:

    set PATH=c:\dev\MinGW\bin\;c:\dev\gtk224\bin
    

    (确保将路径设置为您的 mingw 和 gtk 目录)

    6) 编译:

    c:\dev\MinGW\bin\gcc.exe -g helloworld.c -o helloworld %GTK_INCLUDES% %GTK_LIBS%
    

    7) 当你能够编译成功时,将你在步骤 2、4、5 和 6 中所做的复制到一个批处理文件中,这样就可以通过运行批处理文件来编译你的应用程序。

    【讨论】:

    • 虽然它解决了我的问题。但我很好奇这组 GTK_INCLUDES 和 GTK_LIBS 保存在哪里。我尝试查看环境设置并使用 power shell get-childitem env: ,但找不到这些变量。
    • SET 命令仅将当前会话的值保存在内存中(即您在其中运行SET 的命令窗口)。其他命令窗口将看不到设置的值(除非其他命令窗口是子窗口)。您是否从运行 SET 的同一命令窗口(或从子窗口)运行 PS 命令?
    • 哎呀!是的,我在两个不同的窗口中使用了 cmd 提示符和 ps 提示符。 (不同的情况)。谢谢你的解释。
    【解决方案2】:

    错误就在这里。

    pkg-config 是一个帮助(我强烈推荐)确定链接和库标志的实用程序。你遇到的问题是 gcc 将其解释为参数,如果你像你一样传递它们 - 你需要在 subshel​​l 中执行它们(但我不知道如何在 Windows shell 或 cygwin 下执行此操作)在 bash 下它是 $(pkconfig --libs gtk-2.0) 或带有反引号而不是 $(...)

    D:\gtk>gcc -Wall -g helloworld.c -o helloworld pkg-config --cflags gtk+-2.0 pkg-config --libs gtk+-2.0
    gcc: pkg-config: No such file or directory
    gcc: gtk+-2.0: No such file or directory
    gcc: pkg-config: No such file or directory
    gcc: gtk+-2.0: No such file or directory
    cc1.exe: error: unrecognized command line option "-fcflags"
    cc1.exe: error: unrecognized command line option "-flibs"
    

    【讨论】:

    • 谢谢,我发现了类似的东西:*.com/questions/18799904/…。第九步提到使用不在我的bin文件夹中的bash.exe,但是它再次创建了一个批处理命令,不知道如何在cmd提示符下执行。
    • 我强烈推荐 Cygwin 用于这种情况。使用 Cygwin bash shell 和 MinGW。停止射击自己的脚,并以简单的方式做到这一点。
    • drahnr :您的意见是正确的,倒勾是问题的原因之一。
    【解决方案3】:

    运行 hello, world 程序时出现同样的错误。以下解决方案对我有用。

    不要将您的helloworld.c 保存在任意位置,而是将其放入

    MinGW > msys > 1.0 > home > "你的主文件夹的名称" > helloworld.c

    现在,打开msys.bat 并编写运行程序的命令。在我的情况下是:

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

    它对我有用!

    【讨论】:

      最近更新 更多