2010年7月22日 星期四

GTK+ with Dev-C++ on Windows

前言:
        說到C/C++開發環境,就不能不提到GCC。GCC是GNU Compiler Collection (GNU編譯器總集)的縮寫,為GNU計畫中一套多種程式語言編譯器的集合,在諸多Unix-like與Mac OS X中
都成為其內建的程式開發環境。

        其實,GCC最初的名稱為GNU C Compiler (GNU C 語言編譯器)。在當時,GCC還只是一個專門處理 C 語言的編譯器。而在後來 GCC 擴展之後,慢慢的也可以處理C++、Fortran、
Ada、Java、Objective-C 等語言。發展至今,就是現在我們所看到的編譯器總集了。

        雖然在Unix-like與Mac OS X系統中都已經內建了GCC的環境,但是若要在Windows系統下擁有相同的環境,你可能就需要安裝MinGW了。

        MinGW(Minimalist GNU for Windows),又稱Mingw32,為包含了GCC、GDB(GNU Debugger)、binutils等工具的GNU工具組(toolchain)移植到windows平台上的版本。包括一系列表頭檔(Win32API)、函式庫和可執行檔案。 MinGW是從Cygwin(1.3.3版)基礎上發展而來,但是用MinGW開發的程式不需要額外的第三方DLL支援就可以直接在Windows下執行,而且也不一定必須遵從GPL許可證。

進入正題:
下載Dev-C++ 並安裝
Bloodshed Dev-C++ 4.9.9.2 with MinGW for Windows
下載GTK+ Development Enviroment 並安裝
GTK+ Development Enviroment 2.12.9-2 for Windows

接下來檢查GTK+ Path 是否有正確設定於Dev-C++

執行Dev-C++
點選工具列的「Tools -> Compiler Options」,開啟Compiler Options視窗,
點選「Directories」頁籤,點選「Libraries」頁籤,檢查是否含有以下路徑:C:\GTK\LIB

點選「C Includes」「C++ Includes」頁籤,檢查是否含有以下路徑:
C:\GTK\INCLUDE
C:\GTK\INCLUDE\GTK-2.0
C:\GTK\INCLUDE\GLIB-2.0
C:\GTK\INCLUDE\PANGO-1.0
C:\GTK\INCLUDE\CAIRO
C:\GTK\INCLUDE\ATK-1.0
C:\GTK\INCLUDE\GTKGLEXT-1.0
C:\GTK\LIB\GTK-2.0\INCLUDE
C:\GTK\LIB\GLIB-2.0\INCLUDE
C:\GTK\LIB\GTKGLEXT-1.0\INCLUDE
C:\GTK\INCLUDE\LIBGLADE-2.0
C:\GTK\INCLUDE\LIBXML2
 
建立新專案,在新產生的檔案內輸入第一個GTK+的程式碼:
#include 

int main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello! GTK+!");
gtk_widget_show(window);
gtk_main();
return 0;
}
在Project頁籤內的專案圖示上按右鍵點選「Project Options」點選「Parameters」頁籤
在欄位「Compiler」,輸入以下參數:
-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\pango-1.0 -IC:\GTK\include\glib-2.0 -IC:\GTK\lib\glib-2.0\include -IC:\GTK\include\libpng12
 
在欄位「Linker」,輸入以下參數:
-LC:\GTK\lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl
 
 
compile後執行:
 
compiler參數需要加入-mms-bitfields 的原因:
For reference, here is what the gcc info file has to say about -mms-bitfields:

5.34.3 i386 Variable Attributes
-------------------------------
Two attributes are currently defined for i386 configurations:`ms_struct' and `gcc_struct'
`ms_struct'
`gcc_struct'
If `packed' is used on a structure, or if bit-fields are used it
may be that the Microsoft ABI packs them differently than GCC
would normally pack them. Particularly when moving packed data
between functions compiled with GCC and the native Microsoft
compiler (either via function call or as data in a file), it may
be necessary to access either format.
Currently `-m[no-]ms-bitfields' is provided for the Microsoft
Windows X86 compilers to match the native Microsoft compiler.

沒有留言:

張貼留言