1 <sect1 id="gcc"><title>Using GCC with Cygwin</title>
3 <sect2 id="gcc-cons"><title>Console Mode Applications</title>
5 <para>Use gcc to compile, just like under UNIX.
6 Refer to the GCC User's Guide for information on standard usage and
7 options. Here's a simple example:</para>
9 <example id="gcc-hello-world">
10 <title>Building Hello World with GCC</title>
12 <prompt>bash$</prompt> <userinput>gcc hello.c -o hello.exe</userinput>
13 <prompt>bash$</prompt> <userinput>hello.exe</userinput>
16 <prompt>bash$</prompt>
22 <sect2 id="gcc-gui"><title>GUI Mode Applications</title>
24 <para>Cygwin allows you to build programs with full access to the
25 standard Windows 32-bit API, including the GUI functions as defined in
26 any Microsoft or off-the-shelf publication. However, the process of
27 building those applications is slightly different, as you'll be using
28 the GNU tools instead of the Microsoft tools.</para>
30 <para>For the most part, your sources won't need to change at all.
31 However, you should remove all __export attributes from functions
32 and replace them like this:</para>
35 int foo (int) __attribute__ ((__dllexport__));
41 <para>The Makefile is similar to any other UNIX-like Makefile,
42 and like any other Cygwin makefile. The only difference is that you use
43 <command>gcc -mwindows</command> to link your program into a GUI
44 application instead of a command-line application. Here's an example:</para>
48 myapp.exe : myapp.o myapp.res
49 gcc -mwindows myapp.o myapp.res -o $@
51 myapp.res : myapp.rc resource.h
52 windres $< -O coff -o $@
56 <para>Note the use of <filename>windres</filename> to compile the
57 Windows resources into a COFF-format <filename>.res</filename> file.
58 That will include all the bitmaps, icons, and other resources you
59 need, into one handy object file. Normally, if you omitted the "-O
60 coff" it would create a Windows <filename>.res</filename> format file,
61 but we can only link COFF objects. So, we tell
62 <filename>windres</filename> to produce a COFF object, but for
63 compatibility with the many examples that assume your linker can
64 handle Windows resource files directly, we maintain the
65 <filename>.res</filename> naming convention. For more information on
66 <filename>windres</filename>, consult the Binutils manual. </para>
69 The following is a simple GUI-mode "Hello, World!" program to help
72 /*-------------------------------------------------*/
73 /* hellogui.c - gui hello world */
74 /* build: gcc -mwindows hellogui.c -o hellogui.exe */
75 /*-------------------------------------------------*/
76 #include <windows.h>
80 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
82 int APIENTRY WinMain(HINSTANCE hInstance,
83 HINSTANCE hPrevInstance,
88 "Hello World\nGetCommandLine(): [%s]\n"
89 "WinMain lpCmdLine: [%s]\n",
90 lpCmdLine, GetCommandLine() );
94 wcex.cbSize = sizeof(wcex);
95 wcex.style = CS_HREDRAW | CS_VREDRAW;
96 wcex.lpfnWndProc = WndProc;
99 wcex.hInstance = hInstance;
100 wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
101 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
102 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
103 wcex.lpszMenuName = NULL;
104 wcex.lpszClassName = "HELLO";
107 if (!RegisterClassEx(&wcex))
111 hWnd = CreateWindow("HELLO", "Hello", WS_OVERLAPPEDWINDOW,
112 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
117 ShowWindow(hWnd, nCmdShow);
121 while (GetMessage(&msg, NULL, 0, 0))
123 TranslateMessage(&msg);
124 DispatchMessage(&msg);
130 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
138 hdc = BeginPaint(hWnd, &ps);
140 GetClientRect(hWnd, &rt);
141 DrawText(hdc, glpszText, strlen(glpszText), &rt, DT_TOP | DT_LEFT);
142 EndPaint(hWnd, &ps);
148 return DefWindowProc(hWnd, message, wParam, lParam);