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

C++ 帮助编译GTK+;在Windows上使用MinGW,c++,c,gtk,C++,C,Gtk,我正在尝试在windows上编译一个简单的GTK+Hello World应用程序。以下是此url中的教程: my HelloWorld.c的内容如下所示,也取自维基百科的一个示例: #include <gtk/gtk.h> int main (int argc, char *argv[]) { GtkWidget *window; GtkWidget *label; gtk_init (&argc, &argv); /*

我正在尝试在windows上编译一个简单的GTK+Hello World应用程序。以下是此url中的教程:

my 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;
 }
我的环境路径中有MinGW。在命令提示符内运行以下命令时:

gtkcmd.bat HelloWorld.c HelloWorld.exe
我得到以下输出:

C:\gtk+_compilation>gtkcmd.bat HelloWorld.c HelloWorld.exe

C:\gtk+_compilation>path C:\MinGW\bin;C:C:\gtk_2_22_x64\lib\C:\MinGW\bin;C:C:\gt
k_2_22_x64\lib\C:\MinGW\bin;C:C:\gtk_2_22_x64\lib\C:\MinGW\bin;C:C:\gtk_2_22_x64
\lib\C:\MinGW\bin;C:C:\gtk_2_22_x64\lib\C:\MinGW\bin;C:\gtk_2_22_x64\lib\C:\MinG
W\bin;C:\gtk_2_22_x64\lib\C:\Program Files\Common Files\Microsoft Shared\Windows
 Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windo
ws\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPower
Shell\v1.0\;C:\Program Files\Dell\Dell Wireless WLAN Card;c:\Program Files (x86)
\Common Files\Roxio Shared\10.0\DLLShared\;c:\Program Files (x86)\Common Files\R
oxio Shared\DLLShared\;C:\Program Files\TortoiseSVN\bin;c:\Program Files (x86)\M
icrosoft SQL Server\90\Tools\binn\;C:\Program Files (x86)\Windows Live\Shared;C:
\Program Files (x86)\IronPython 2.7;c:\Program Files (x86)\Microsoft ASP.NET\ASP
.NET Web Pages\v1.0\;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\MinGW\bin;C:\gt
k_2_22_x64\bin;C:\gtk_2_22_x64\lib

C:\gtk+_compilation>gcc -Wall -g HelloWorld.c -o HelloWorld.exe -mno-cygwin -mms
-bitfields -IC:C:\gtk_2_22_x64\include\gtk-2.0 -IC:C:\gtk_2_22_x64\lib\gtk-2.0\i
nclude -IC:C:\gtk_2_22_x64\include\atk-1.0 -IC:C:\gtk_2_22_x64\include\cairo -IC
:C:\gtk_2_22_x64\include\gdk-pixbuf-2.0 -IC:C:\gtk_2_22_x64\include\pango-1.0 -I
C:C:\gtk_2_22_x64\include\glib-2.0 -IC:C:\gtk_2_22_x64\lib\glib-2.0\include -IC:
C:\gtk_2_22_x64\include\pixman-1 -IC:C:\gtk_2_22_x64\include -IC:C:\gtk_2_22_x64
\include\freetype2 -IC:C:\gtk_2_22_x64\include\libpng14 -LC:C:\gtk_2_22_x64\lib
-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>
以前我严格按照教程所说的做了尝试,并在path语句中使用了/而不是我现在使用的完整路径。当我使用/时,尽管它无法链接任何内容。每个GTK语句都返回一个未定义的错误。所以我一直坚持在header中使用完整路径,在命令行中使用linkers语句。从上面可以看到,它找不到gtk/gth.h头。这让我很困惑,因为我可以在命令行参数中清楚地看到这些参数的路径

非常感谢您的帮助

干杯

安德鲁

更新:

好的-我确实把bat文件搞砸了-愚蠢的错误,但在用下面的答案更正这些错误后,我现在有了以下bat文件:

path \MinGW\bin;C:\gtk_2_22_x64\lib\%path%

gcc -Wall -g %1 -o %2 -mno-cygwin -mms-bitfields -IC:\gtk_2_22_x64\include\gtk-2.0 -IC:\gtk_2_22_x64\lib\gtk-2.0\include -IC:\gtk_2_22_x64\include\atk-1.0 -IC:\gtk_2_22_x64\include\cairo -IC:\gtk_2_22_x64\include\gdk-pixbuf-2.0 -IC:\gtk_2_22_x64\include\pango-1.0 -IC:\gtk_2_22_x64\include\glib-2.0 -IC:\gtk_2_22_x64\lib\glib-2.0\include -IC:\gtk_2_22_x64\include\pixman-1 -IC:\gtk_2_22_x64\include -IC:\gtk_2_22_x64\include\freetype2 -IC:\gtk_2_22_x64\include\libpng14 -LC:\gtk_2_22_x64\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


%2
在此之后,我现在从编译中得到以下错误:

C:\Users\REA_AN~1\AppData\Local\Temp\cclrbtyP.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_x64\include\gtk-2.0
应该是:

-IC:\gtk_2_22_x64\include\gtk-2.0
path C:\MinGW\bin;C:\gtk_2_22_x64\lib\%path%
其他地方也类似。看起来您的路径设置不正确-这是:

path C:\MinGW\bin;C:C:\gtk_2_22_x64\lib\%path%
应该是:

-IC:\gtk_2_22_x64\include\gtk-2.0
path C:\MinGW\bin;C:\gtk_2_22_x64\lib\%path%
或者类似的东西(注意C:C:应该是C:)

编辑:现在的问题是链接器找不到库。批处理文件应包含以下内容:

-L/path/to/gtk/libraries

但我不知道您在创建文件时是如何指定的。

我已经用您指出的更正更新了我的问题+1对于更正,谢谢:)这在上面的.bat文件内容中存在-立法会:gtk_2_22_x64\lib@REA_哦,对不起,我错过了。但这些绝对是链接器错误,因此似乎出于某种原因找不到它所需的库。您不需要硬编码的路径依赖关系。使用'pkg config--cflags gtk+-2.0',等等。