OSDN Git Service

* new-features.sgml (ov-new1.7.2): Accommodate name change of getlocale
[pf3gnuchains/pf3gnuchains4x.git] / winsup / doc / gcc.sgml
1 <sect1 id="gcc"><title>Using GCC with Cygwin</title>
2
3 <sect2 id="gcc-cons"><title>Console Mode Applications</title>
4
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>
8
9 <example id="gcc-hello-world">
10 <title>Building Hello World with GCC</title>
11 <screen>
12 <prompt>bash$</prompt> <userinput>gcc hello.c -o hello.exe</userinput>
13 <prompt>bash$</prompt> <userinput>hello.exe</userinput>
14 Hello, World
15
16 <prompt>bash$</prompt>
17 </screen>
18 </example>
19
20 </sect2>
21
22 <sect2 id="gcc-gui"><title>GUI Mode Applications</title>
23
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>
29
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>
33
34 <screen>
35 int foo (int) __attribute__ ((__dllexport__));
36
37 int
38 foo (int i)
39 </screen>
40
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>
45
46 <screen>
47 <![CDATA[
48 myapp.exe : myapp.o myapp.res
49         gcc -mwindows myapp.o myapp.res -o $@
50
51 myapp.res : myapp.rc resource.h
52         windres $< -O coff -o $@
53 ]]>
54 </screen>
55
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>
67
68 <para>
69 The following is a simple GUI-mode "Hello, World!" program to help
70 get you started:
71 <screen>
72 /*-------------------------------------------------*/
73 /* hellogui.c - gui hello world                    */
74 /* build: gcc -mwindows hellogui.c -o hellogui.exe */
75 /*-------------------------------------------------*/
76 #include &lt;windows.h&gt;
77
78 char glpszText[1024];
79
80 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
81
82 int APIENTRY WinMain(HINSTANCE hInstance, 
83                 HINSTANCE hPrevInstance,
84                 LPSTR lpCmdLine,
85                 int nCmdShow)
86 {
87         sprintf(glpszText, 
88                 "Hello World\nGetCommandLine(): [%s]\n"
89                 "WinMain lpCmdLine: [%s]\n",
90                 lpCmdLine, GetCommandLine() );
91
92         WNDCLASSEX wcex; 
93  
94         wcex.cbSize = sizeof(wcex);
95         wcex.style = CS_HREDRAW | CS_VREDRAW;
96         wcex.lpfnWndProc = WndProc;
97         wcex.cbClsExtra = 0;
98         wcex.cbWndExtra = 0;
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";
105         wcex.hIconSm = NULL;
106
107         if (!RegisterClassEx(&amp;wcex))
108                 return FALSE; 
109
110         HWND hWnd;
111         hWnd = CreateWindow("HELLO", "Hello", WS_OVERLAPPEDWINDOW,
112                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
113
114         if (!hWnd)
115                 return FALSE;
116
117         ShowWindow(hWnd, nCmdShow);
118         UpdateWindow(hWnd);
119
120         MSG msg;
121         while (GetMessage(&amp;msg, NULL, 0, 0)) 
122         {
123                 TranslateMessage(&amp;msg);
124                 DispatchMessage(&amp;msg);
125         }
126
127         return msg.wParam;
128 }
129
130 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
131 {
132         PAINTSTRUCT ps;
133         HDC hdc;
134         
135         switch (message) 
136         {
137                 case WM_PAINT:
138                         hdc = BeginPaint(hWnd, &amp;ps);
139                         RECT rt;
140                         GetClientRect(hWnd, &amp;rt);
141                         DrawText(hdc, glpszText, strlen(glpszText), &amp;rt, DT_TOP | DT_LEFT);
142                         EndPaint(hWnd, &amp;ps);
143                         break;
144                 case WM_DESTROY:
145                         PostQuitMessage(0);
146                         break;
147                 default:
148                         return DefWindowProc(hWnd, message, wParam, lParam);
149         }
150         return 0;
151 }
152 </screen>
153 </para>
154
155 </sect2>
156 </sect1>