Using GCC with Cygwin
Console Mode Applications
Use gcc to compile, just like under UNIX.
Refer to the GCC User's Guide for information on standard usage and
options. Here's a simple example:
Building Hello World with GCC
bash$ gcc hello.c -o hello.exe
bash$ hello.exe
Hello, World
bash$
GUI Mode Applications
Cygwin allows you to build programs with full access to the
standard Windows 32-bit API, including the GUI functions as defined in
any Microsoft or off-the-shelf publication. However, the process of
building those applications is slightly different, as you'll be using
the GNU tools instead of the Microsoft tools.
For the most part, your sources won't need to change at all.
However, you should remove all __export attributes from functions
and replace them like this:
int foo (int) __attribute__ ((__dllexport__));
int
foo (int i)
The Makefile is similar to any other UNIX-like Makefile,
and like any other Cygwin makefile. The only difference is that you use
gcc -mwindows to link your program into a GUI
application instead of a command-line application. Here's an example:
Note the use of windres to compile the
Windows resources into a COFF-format .res file.
That will include all the bitmaps, icons, and other resources you
need, into one handy object file. Normally, if you omitted the "-O
coff" it would create a Windows .res format file,
but we can only link COFF objects. So, we tell
windres to produce a COFF object, but for
compatibility with the many examples that assume your linker can
handle Windows resource files directly, we maintain the
.res naming convention. For more information on
windres, consult the Binutils manual.
The following is a simple GUI-mode "Hello, World!" program to help
get you started:
/*-------------------------------------------------*/
/* hellogui.c - gui hello world */
/* build: gcc -mwindows hellogui.c -o hellogui.exe */
/*-------------------------------------------------*/
#include <windows.h>
char glpszText[1024];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
sprintf(glpszText,
"Hello World\nGetCommandLine(): [%s]\n"
"WinMain lpCmdLine: [%s]\n",
lpCmdLine, GetCommandLine() );
WNDCLASSEX wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "HELLO";
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex))
return FALSE;
HWND hWnd;
hWnd = CreateWindow("HELLO", "Hello", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, glpszText, strlen(glpszText), &rt, DT_TOP | DT_LEFT);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}