系统:Windows7,32位,GTK 2.24.10,mingw
我正在尝试编写基本的helloworld.ctypes的基于GTK的应用程序。 但是,它不运行。
这是我遵循的步骤。
D:\ gtk> gcc -o project helloworld.c%GTK%
gcc:%GTK%:没有这样的文件或目录helloworld.c:1:21:错误:gtk / gtk.h:没有这样的文件或目录helloworld.c:在函数'main':helloworld.c:5:error: 'GtkWidget'未声明(在此函数中首次使用)helloworld.c:5:错误:(每个未声明的标识符只会报告一次helloworld.c:5:错误:对于每个函数都会显示。)helloworld.c:5:error :'窗口'未声明(首次在此函数中使用)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"
batch file在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; }
参考: 在Windows下使用gcc安装gtk和编译?
您可以尝试以下手动步骤来开始:
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中的内容复制到批处理文件中,这样只需运行批处理文件即可编译你的应用程序。
错误在这里。
pkg-config
是一个实用工具,它可以帮助确定链接和lib标志(我强烈建议)。 你得到的问题是, 海湾合作委员会将它解释为一个参数,如果你像你一样传递它们 – 你需要在一个子shell中执行它们(但是我不知道如何在windows shell或cygwin下做到这一点) $(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程序时得到同样的错误。 以下解决方案为我工作。
不要把你的helloworld.c
放在任何地方,放在里面
MinGW> msys> 1.0>首页>“您的主文件夹的名称”> helloworld.c
现在,打开msys.bat
并编写命令来运行程序。 在我的情况是这样的:
gcc hello.c -o hello `pkg-config --cflags --libs gtk+-3.0`
它适用于我!