簡體   English   中英

在Windows 7上編譯和運行GTK +應用程序

[英]Compiling and running GTK+ application on Windows 7

系統: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 = FLAGS start cmd,其中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;
}

參考: 在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)當你能夠編譯OK時,將你在步驟2,4,5和6中所做的事情復制到批處理文件中,這樣就可以通過運行批處理文件來編譯app。

錯誤就在這里。

pkg-config是一個幫助(我強烈建議)確定鏈接和lib標志的實用程序。 你得到的問題是gcc將它解釋為參數,如果你像你一樣傳遞它們 - 你需要在子shell中對它們進行解釋(但我不知道如何在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程序時會出現同樣的錯誤。 以下解決方案為我工作。

而不是在任何地方保存你的helloworld.c ,把它放在里面

MinGW> msys> 1.0> home>“您的主文件夾的名稱”> helloworld.c

現在,打開msys.bat並編寫命令來運行程序。 在我的情況下它是:

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

它對我有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM