Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gcc.sgml « doc « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a67865de3e4212be19ce2f3ddf18de2c7815d1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<sect1 id="gcc"><title>Using GCC with Cygwin</title>

<sect2 id="gcc-cons"><title>Console Mode Applications</title>

<para>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:</para>

<example>
<title>Building Hello World with GCC</title>
<screen>
<prompt>C:\&gt;</prompt> <userinput>gcc hello.c -o hello.exe</userinput>
<prompt>C:\&gt;</prompt> <userinput>hello.exe</userinput>
Hello, World

<prompt>C:\&gt;</prompt>
</screen>
</example>

</sect2>

<sect2 id="gcc-gui"><title>GUI Mode Applications</title>

<para>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.</para>

<para>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:</para>

<screen>
int foo (int) __attribute__ ((__dllexport__));

int
foo (int i)
</screen>

<para>The Makefile is similar to any other UNIX-like Makefile,
and like any other Cygwin makefile.  The only difference is that you use
<command>gcc -mwindows</command> to link your program into a GUI
application instead of a command-line application.  Here's an example:</para>

<screen>
myapp.exe : myapp.o myapp.res
	gcc -mwindows myapp.o myapp.res -o $@

myapp.res : myapp.rc resource.h
	windres $< -O coff -o $@
</screen>

<para>Note the use of <filename>windres</filename> to compile the
Windows resources into a COFF-format <filename>.res</filename> 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 <filename>.res</filename> format file,
but we can only link COFF objects.  So, we tell
<filename>windres</filename> 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
<filename>.res</filename> naming convention.  For more information on
<filename>windres</filename>, consult the Binutils manual.  </para>

<para>
The following is a simple GUI-mode "Hello, World!" program to help
get you started:
<screen>
/*-------------------------------------------------*/
/* hellogui.c - gui hello world                    */
/* build: gcc -mwindows hellogui.c -o hellogui.exe */
/*-------------------------------------------------*/
#include &lt;windows.h&gt;

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(&amp;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(&amp;msg, NULL, 0, 0)) 
	{
		TranslateMessage(&amp;msg);
		DispatchMessage(&amp;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, &amp;ps);
			RECT rt;
			GetClientRect(hWnd, &amp;rt);
			DrawText(hdc, glpszText, strlen(glpszText), &amp;rt, DT_TOP | DT_LEFT);
			EndPaint(hWnd, &amp;ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
</screen>
</para>

</sect2>
</sect1>