帮助在Windows上使用MinGW编译GTK+

Help compiling GTK+ on Windows using MinGW

本文关键字:MinGW 编译 GTK+ Windows 帮助      更新时间:2023-10-16

我正在尝试在windows上编译一个简单的GTK+ Hello World应用程序。我正在遵循这个url的教程:

http://pandhare0.tripod.com/mini-Tutorial

我的HelloWorld.c的内容如下,也取自维基百科的例子:

 #include <gtk/gtk.h>
 int main (int argc, char *argv[])
 {
    GtkWidget *window;
    GtkWidget *label;
    gtk_init (&argc, &argv);
    /* create the main, top level, window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    /* give it the title */
    gtk_window_set_title (GTK_WINDOW (window), "Hello World");
    /* Connect the destroy signal of the window to gtk_main_quit
     * When the window is about to be destroyed we get a notification and
     * stop the main GTK+ loop
     */
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    /* Create the "Hello, World" label  */
    label = gtk_label_new ("Hello, World");
    /* and insert it into the main window  */
    gtk_container_add (GTK_CONTAINER (window), label);
    /* make sure that everything, window and label, are visible */
    gtk_widget_show_all (window);
    /* start the main loop, and let it rest there until the application is closed */
    gtk_main ();
    return 0;
 }

我已经按照说明生成了build bat文件,其内容如下:

path C:MinGWbin;C:C:gtk_2_22_x64lib%path%
gcc -Wall -g %1 -o %2 -mno-cygwin -mms-bitfields -IC:C:gtk_2_22_x64includegtk-2.0 -IC:C:gtk_2_22_x64libgtk-2.0include -IC:C:gtk_2_22_x64includeatk-1.0 -IC:C:gtk_2_22_x64includecairo -IC:C:gtk_2_22_x64includegdk-pixbuf-2.0 -IC:C:gtk_2_22_x64includepango-1.0 -IC:C:gtk_2_22_x64includeglib-2.0 -IC:C:gtk_2_22_x64libglib-2.0include -IC:C:gtk_2_22_x64includepixman-1 -IC:C:gtk_2_22_x64include -IC:C:gtk_2_22_x64includefreetype2 -IC:C:gtk_2_22_x64includelibpng14 -LC:C:gtk_2_22_x64lib -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

%2

在我的环境路径中有MinGW。当我在命令提示符中运行以下命令时:

gtkcmd.bat HelloWorld.c HelloWorld.exe

得到以下输出:

C:gtk+_compilation>gtkcmd.bat HelloWorld.c HelloWorld.exe
C:gtk+_compilation>path C:MinGWbin;C:C:gtk_2_22_x64libC:MinGWbin;C:C:gt
k_2_22_x64libC:MinGWbin;C:C:gtk_2_22_x64libC:MinGWbin;C:C:gtk_2_22_x64
libC:MinGWbin;C:C:gtk_2_22_x64libC:MinGWbin;C:gtk_2_22_x64libC:MinG
Wbin;C:gtk_2_22_x64libC:Program FilesCommon FilesMicrosoft SharedWindows
 Live;C:Program Files (x86)Common FilesMicrosoft SharedWindows Live;C:Windo
wssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPower
Shellv1.0;C:Program FilesDellDell Wireless WLAN Card;c:Program Files (x86)
Common FilesRoxio Shared10.0DLLShared;c:Program Files (x86)Common FilesR
oxio SharedDLLShared;C:Program FilesTortoiseSVNbin;c:Program Files (x86)M
icrosoft SQL Server90Toolsbinn;C:Program Files (x86)Windows LiveShared;C:
Program Files (x86)IronPython 2.7;c:Program Files (x86)Microsoft ASP.NETASP
.NET Web Pagesv1.0;C:Program Files (x86)GtkSharp2.12bin;C:MinGWbin;C:gt
k_2_22_x64bin;C:gtk_2_22_x64lib
C:gtk+_compilation>gcc -Wall -g HelloWorld.c -o HelloWorld.exe -mno-cygwin -mms
-bitfields -IC:C:gtk_2_22_x64includegtk-2.0 -IC:C:gtk_2_22_x64libgtk-2.0i
nclude -IC:C:gtk_2_22_x64includeatk-1.0 -IC:C:gtk_2_22_x64includecairo -IC
:C:gtk_2_22_x64includegdk-pixbuf-2.0 -IC:C:gtk_2_22_x64includepango-1.0 -I
C:C:gtk_2_22_x64includeglib-2.0 -IC:C:gtk_2_22_x64libglib-2.0include -IC:
C:gtk_2_22_x64includepixman-1 -IC:C:gtk_2_22_x64include -IC:C:gtk_2_22_x64
includefreetype2 -IC:C:gtk_2_22_x64includelibpng14 -LC:C:gtk_2_22_x64lib
-lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lp
angocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -
lgthread-2.0 -lglib-2.0 -lintl
HelloWorld.c:1:22: fatal error: gtk/gtk.h: No such file or directory
compilation terminated.
C:gtk+_compilation>HelloWorld.exe
'HelloWorld.exe' is not recognized as an internal or external command,
operable program or batch file.
C:gtk+_compilation>

之前我已经尝试过,严格按照教程所说的,在路径语句中使用/,而不是我现在使用的完整路径。当我使用/时,虽然它不能连接任何东西。每个GTK语句返回一个未定义的错误。因此,我一直使用标题中的完整路径和命令行中的链接器语句。从上面可以看到,它找不到gtk/gth.h头文件。这让我很困惑,因为我可以清楚地看到命令行参数中的路径。

感谢您的帮助。

欢呼,安德鲁

更新:

Ok -我确实搞砸了bat文件-愚蠢的错误,但在纠正这些内联的答案之后,我现在有以下bat文件:

path MinGWbin;C:gtk_2_22_x64lib%path%
gcc -Wall -g %1 -o %2 -mno-cygwin -mms-bitfields -IC:gtk_2_22_x64includegtk-2.0 -IC:gtk_2_22_x64libgtk-2.0include -IC:gtk_2_22_x64includeatk-1.0 -IC:gtk_2_22_x64includecairo -IC:gtk_2_22_x64includegdk-pixbuf-2.0 -IC:gtk_2_22_x64includepango-1.0 -IC:gtk_2_22_x64includeglib-2.0 -IC:gtk_2_22_x64libglib-2.0include -IC:gtk_2_22_x64includepixman-1 -IC:gtk_2_22_x64include -IC:gtk_2_22_x64includefreetype2 -IC:gtk_2_22_x64includelibpng14 -LC:gtk_2_22_x64lib -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

%2
在此之后,我现在从编译中得到以下错误:
C:UsersREA_AN~1AppDataLocalTempcclrbtyP.o: In function `main':
C:gtk+_compilation/HelloWorld.c:8: undefined reference to `gtk_init_abi_check'
C:gtk+_compilation/HelloWorld.c:11: undefined reference to `gtk_window_new'
C:gtk+_compilation/HelloWorld.c:14: undefined reference to `gtk_window_get_type
'
C:gtk+_compilation/HelloWorld.c:14: undefined reference to `g_type_check_instan
ce_cast'
C:gtk+_compilation/HelloWorld.c:14: undefined reference to `gtk_window_set_titl
e'
C:gtk+_compilation/HelloWorld.c:20: undefined reference to `gtk_main_quit'
C:gtk+_compilation/HelloWorld.c:20: undefined reference to `g_signal_connect_da
ta'
C:gtk+_compilation/HelloWorld.c:23: undefined reference to `gtk_label_new'
C:gtk+_compilation/HelloWorld.c:26: undefined reference to `gtk_container_get_t
ype'
C:gtk+_compilation/HelloWorld.c:26: undefined reference to `g_type_check_instan
ce_cast'
C:gtk+_compilation/HelloWorld.c:26: undefined reference to `gtk_container_add'
C:gtk+_compilation/HelloWorld.c:29: undefined reference to `gtk_widget_show_all
'
C:gtk+_compilation/HelloWorld.c:32: undefined reference to `gtk_main'
collect2: ld returned 1 exit status

似乎您按照说明不正确地创建了.bat文件-这:

-IC:C:gtk_2_22_x64includegtk-2.0
应:

-IC:gtk_2_22_x64includegtk-2.0

和其他类似的。看起来你的路径设置不正确- this:

path C:MinGWbin;C:C:gtk_2_22_x64lib%path%
应:

path C:MinGWbin;C:gtk_2_22_x64lib%path%

或类似的东西(注意C:C:应该是C:)。

Edit:现在你的问题是链接器找不到库。批处理文件应该包含如下内容:

-L/path/to/gtk/libraries

但是我不知道你在创建文件的时候是怎么指定的