好久没有发表文章了.哈哈...因为最近比较忙..之前在准备考研.:).........
由于最近地震,把光缆震坏...随之而来的是KAV无法更新...中毒...5555搞到单单为重装系统就花了我一整天.
装完系统,就想装Visual Studio.但是由于没有英文版,光驱也坏了....而且想到如果以后系统又重装..那么又要装一个足够庞大的" Visual Studio"那不是很浪费时间.于是我决定...不使用Visual Studio,选择一轻便的编程工具.
于是,...在机缘巧合下,我发现了MINGW..其实一年前就装了但一直没有使用.于是,就有设想,能不能把GNU下的开发工具都搬过WINDOWS下,搭建一个类"LINUX"开发的WINDOWS开发平台.这样,对于以后系统崩溃和学习GNU开源库都是有发处的.以下便是我搭建过程.
都是地震的缘故,由于光缆坏了,通过台湾代理的服务器无法访问到sourceforge主页[就算可以也很慢]最后我只能通过google绕过sourceforge主页,来到其后台下载.以下给出我下载的地址:
http://mirror.in.th/sourceforge.net/m/mi/mingw/
里面列出所有的mingw官方主页介绍的所有mingw相关库[当然也包括msys].里面有一个mingw.5.1.1exe .[最新是minw5.1.2]它是用于由用户定制自动访问站点和安装的工具.但由于mingw在这方面还不成熟,而且现在光缆损坏要下载也很难连接.所以我选择直在上面提供的链接下通过迅雷下载:那应该下载相关文件呢,这可以参考mingw的安装说明,在其主页上有提供:
http://www.mingw.org/docs.shtml
下面我列出我所下载的列表:[方便难以访问其主页的朋友]
binutils-2.17.50-20060824-1.tar.gz
gcc-core-3.4.5-20060117-1.rar
gcc-g++-3.4.5-20060117-1.tar.gz
mingw-runtime-3.11.rar
w32api-3.8.rar
gdb-6.3-2.exe
以上文件就可以提供基本的C/C++编程.当然有兴趣的朋友也可以下载
gcc-g77-3.4.5-20060117-1.rar
gcc-java-3.4.5-20060117-1.tar.gz
gcc-objc-3.4.5-20060117-1.rar
以提供更多的编程选择.
当下载完基本的mingW文件时,那么我们就可以安装了.安装过程十分简单.
首先,在你想安装的位置新建一个目录.而我选择的是G:/Tool/Mingw
接着,把所有文件复制到上面新建的文件夹.如果不想保留下载的文件直接可以剪切.
下来,就是重复性的工作,把下载的基本文件解压到当前文件夹.而我是采用rar解压,这里要记住选择"解压到
当前文件夹",这样才能把所有下载文件整合.
当然也可以先下载msys和GNUWIN32的解压缩工具,编写脚本来自动完成上述步骤.[脚本在MINGW的官方主页
有提供一个参考]
由于文件不多,我也就没有使用脚本.
最后一步,就是要设置环境变量.我是把path设为...;G:/Tool/MingW/bin,这要根据大家安装的不同位置作出相应调整就可了.
现在测试,在"运行"->"cmd"->打入"gcc"如果出现
gcc: no input files
那么恭喜你,MINGW已经安装成功,你已经拥有基本的编译器去进行编程.
如果需要模拟LINUX环境,对GUN源码进行编译,可以下载
MSYS-1.0.11-2004.04.30-1.exe
安装就可以. 这一步安装很简单..next->next...就可以完成了,我在这里就不详述了.
至于GNUWIN32,里面提供了很多从LINUX下转成WINDOWS的实用工具,如
FLXE,BISON,Gperf,make[其实MingW也有提供,我是使用GNUWIN32的].automake,..........
这些就可以由朋友你自已根据需要下载了.但是同样,它需要访问sourceforge,如果使用台湾代理就会很慢,甚至打不开.这里我提供一个有关所有GNUWIN32的下载列表.方便大家下载.
http://sourceforge.net/project/showfiles.php?group_id=23617
最好建议不要使用台湾的代理服务器,因为可能光缆还没有完全修复,下载会很慢.
下载有许多可选,如果不需要阅读代码建议下载Setup,安装方便.
当然第一次安装GNUWIN32,也要设置环境变量
就这样我们就简单地搭建好一个GNUWIN compiling.
最后我们有很多编辑器选择,我选择的是EditPlus2,比较小巧方便.当然我也配置了eclipse,在调试方面可能eclipse
更有优势,但一般编程则显过于庞大,而且其运行也很慢.
以下给出两个我的编译例子,代码一来自于<<Programming Win32 with GNU C and C++>>,一来自libconfig的manual.以便初次使用MINGW和GNUWIn32的朋友有一个完整的认识.
HelloWorld的Windwos界面版:
/*
* A basic example of Win32 programming in C.
*
* This source code is in the PUBLIC DOMAIN and has NO WARRANTY.
*
* Colin Peters <colinpeters at geocities.com>
*/
#include <windows.h>
#include <string.h>
/*
* This is the window function for the main window. Whenever a message is
* dispatched using DispatchMessage (or sent with SendMessage) this function
* gets called with the contents of the message.
*/
LRESULT CALLBACK
MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
/* The window handle for the "Click Me" button. */
static HWND hwndButton = 0;
static int cx, cy; /* Height and width of our button. */
HDC hdc; /* A device context used for drawing */
PAINTSTRUCT ps; /* Also used during window drawing */
RECT rc; /* A rectangle used during drawing */
/*
* Perform processing based on what kind of message we got.
*/
switch (nMsg)
{
case WM_CREATE:
{
/* The window is being created. Create our button
* window now. */
TEXTMETRIC tm;
/* First we use the system fixed font size to choose
* a nice button size. */
hdc = GetDC (hwnd);
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
GetTextMetrics (hdc, &tm);
cx = tm.tmAveCharWidth * 30;
cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
ReleaseDC (hwnd, hdc);
/* Now create the button */
hwndButton = CreateWindow (
"button", /* Builtin button class */
"Click Here",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, cx, cy,
hwnd, /* Parent is this window. */
(HMENU) 1, /* Control ID: 1 */
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
return 0;
break;
}
case WM_DESTROY:
/* The window is being destroyed, close the application
* (the child button gets destroyed automatically). */
PostQuitMessage (0);
return 0;
break;
case WM_PAINT:
/* The window needs to be painted (redrawn). */
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rc);
/* Draw "Hello, World" in the middle of the upper
* half of the window. */
rc.bottom = rc.bottom / 2;
DrawText (hdc, "Hello, World", -1, &rc,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint (hwnd, &ps);
return 0;
break;
case WM_SIZE:
/* The window size is changing. If the button exists
* then place it in the center of the bottom half of
* the window. */
if (hwndButton &&
(wParam == SIZEFULLSCREEN ||
wParam == SIZENORMAL)
)
{
rc.left = (LOWORD(lParam) - cx) / 2;
rc.top = HIWORD(lParam) * 3 / 4 - cy / 2;
MoveWindow (
hwndButton,
rc.left, rc.top, cx, cy, TRUE);
}
break;
case WM_COMMAND:
/* Check the control ID, notification code and
* control handle to see if this is a button click
* message from our child button. */
if (LOWORD(wParam) == 1 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == hwndButton)
{
/* Our button was clicked. Close the window. */
DestroyWindow (hwnd);
}
return 0;
break;
}
/* If we don't handle a message completely we hand it to the system
* provided default window function. */
return DefWindowProc (hwnd, nMsg, wParam, lParam);
}
int STDCALL
WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
HWND hwndMain; /* Handle for the main window. */
MSG msg; /* A Win32 message structure. */
WNDCLASSEX wndclass; /* A window class structure. */
char* szMainWndClass = "WinTestWin";
/* The name of the main window class */
/*
* First we create a window class for our main window.
*/
/* Initialize the entire structure to zero. */
memset (&wndclass, 0, sizeof(WNDCLASSEX));
/* This class is called WinTestWin */
wndclass.lpszClassName = szMainWndClass;
/* cbSize gives the size of the structure for extensibility. */
wndclass.cbSize = sizeof(WNDCLASSEX);
/* All windows of this class redraw when resized. */
wndclass.style = CS_HREDRAW | CS_VREDRAW;
/* All windows of this class use the MainWndProc window function. */
wndclass.lpfnWndProc = MainWndProc;
/* This class is used with the current program instance. */
wndclass.hInstance = hInst;
/* Use standard application icon and arrow cursor provided by the OS */
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
/* Color the background white */
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
/*
* Now register the window class for use.
*/
RegisterClassEx (&wndclass);
/*
* Create our main window using that window class.
*/
hwndMain = CreateWindow (
szMainWndClass, /* Class name */
"Hello", /* Caption */
WS_OVERLAPPEDWINDOW, /* Style */
CW_USEDEFAULT, /* Initial x (use default) */
CW_USEDEFAULT, /* Initial y (use default) */
CW_USEDEFAULT, /* Initial x size (use default) */
CW_USEDEFAULT, /* Initial y size (use default) */
NULL, /* No parent window */
NULL, /* No menu */
hInst, /* This program instance */
NULL /* Creation parameters */
);
/*
* Display the window which we just created (using the nShow
* passed by the OS, which allows for start minimized and that
* sort of thing).
*/
ShowWindow (hwndMain, nShow);
UpdateWindow (hwndMain);
/*
* The main message loop. All messages being sent to the windows
* of the application (or at least the primary thread) are retrieved
* by the GetMessage call, then translated (mainly for keyboard
* messages) and dispatched to the appropriate window procedure.
* This is the simplest kind of message loop. More complex loops
* are required for idle processing or handling modeless dialog
* boxes. When one of the windows calls PostQuitMessage GetMessage
* will return zero and the wParam of the message will be filled
* with the argument to PostQuitMessage. The loop will end and
* the application will close.
*/
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
保存为test.c
编译命令:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:/Documents and Settings/Q.W.F/桌面>gcc -c test.c -o test.o
C:/Documents and Settings/Q.W.F/桌面>gcc -o"test.exe" test.o -L./ -lgdi32
因为使用到gdi32.lib这个库,所以在链接时我们需要指定.在网上曾看过,Win32的库.lib与MingW的库.a是相兼容的
不过我还深究.这个有待探讨,有了解的朋友希望能指出一起学习啦~
至于第二个例子,我们通过-I,-L选项指定调有GUNWIN32的里面的相关库,我之所以分开是因为这样会更容易管理
在很大安装中都是把相关库的头文件和库文件全部copy到mingw中,我觉得这是不优雅的.
#include <libconfig.h>
#include <stdlib.h>
#include <stdio.h>
int
main (int argc, char **argv)
{
int lc_p_ret, lc_rv_ret;
char *filename = "test.cfg";
lc_rv_ret = lc_register_var ("File", LC_VAR_STRING, &filename, 'f');
if (lc_rv_ret != 0)
{
fprintf (stderr, "Error registering variable: %i./n", lc_geterrno ());
return (EXIT_FAILURE);
}
lc_p_ret = lc_process (argc, argv, "example", LC_CONF_APACHE, NULL);
lc_cleanup ();
if (lc_p_ret != 0)
{
fprintf (stderr, "Error processing configuration: /%s/n", lc_geterrstr ());
return (EXIT_FAILURE);
}
if (filename != NULL)
{
printf ("File specified was: %s/n", filename);
}
else
{
printf ("No filename specified./n");
}
return (EXIT_SUCCESS);
}
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:/Documents and Settings/Q.W.F/桌面>gcc -c Hello.c -IG:/Tool/GnuWin32/include
C:/Documents and Settings/Q.W.F/桌面>gcc -o"Hello.exe" Hello.o -L"G:/Tool/GnuWin
32/lib" -lconfig
这里说明了如何在MinuGW调用GnuWin32的库.这里需要说明的是当调用外部库时,顺序是很重要,之前我就在这里
搞了很久.在网上也看到很多朋友无法解决这种问题..不过还好,最近明白是什么原因.
好了暂时就写这么多吧,下一个目标就打算集成gtk+了呵呵~