在 Windows 7 上编译和运行 GTK+ 应用程序

Compiling and running GTK+ application on Windows 7

本文关键字:运行 GTK+ 应用程序 编译 Windows      更新时间:2023-10-16

系统: 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=标志启动 CMD其中 VAR = GTK,FLAGS = 上一个命令的输出(pkg 配置)。当你想编译文件使用命令:gcc foo.c %VAR%

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

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

你好世界

#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;
}

参考 : 在 Windows 下安装 gtk 并使用 gcc 进行编译?

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

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

c:devgtk224binpkg-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:dev1_repogtk_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:devgtk224binpkg-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:dev1_repogtk_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:devMinGWbin;c:devgtk224bin

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

6)编译:

c:devMinGWbingcc.exe -g helloworld.c -o helloworld %GTK_INCLUDES% %GTK_LIBS%

7) 当您能够编译确定时,将您在步骤 2、4、5 和 6 中所做的复制到批处理文件中,以便只需运行批处理文件即可编译您的应用程序。

错误就在这里。

pkg-config是一个实用程序,可帮助(我强烈建议)确定链接和库标志。你得到的问题是,如果你像你一样传递它们,gcc 会将其解释为一个参数 - 你需要在子壳中引用它们(但我不知道如何在 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"

运行时出现相同的错误 hello, world program。以下解决方案为我工作。

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

MinGW> msys> 1.0> Home> "Your Home Folder的名称"> helloworld.c

现在,打开msys.bat并编写命令以运行程序。就我而言,它是:

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

它对我有用!