gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:40:49 27 4
gpt4 key购买 nike

系统: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. 它打印编译标志列表和链接你的库项目到。复制它们并创建一个 bath 文件,如下所示。设置 VAR=FLAGS开始命令其中 VAR = GTK,FLAGS = 上一个命令 (pkg-config) 的输出。当你想编译文件时使用命令: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: error: 'window' 未声明(首次在此函数中使用)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

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?

最佳答案

您可以先尝试这些手动步骤:

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) 当您能够编译 OK 时,将您在步骤 2、4、5 和 6 中所做的复制到批处理文件中,这样只需运行批处理文件即可编译您的应用。

关于c++ - 在 Windows 7 上编译和运行 GTK+ 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19325552/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com