blob: 3c487768b4a600719058725d7a0d8f7c635d7dc5 (
plain) (
blame)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<meta charset='utf-8'>
<title>C & GDB</title>
</head>
<body>
<a href="../index.html">Development Index</a>
<h1>C & GDB</h1>
<ul>
<li><a href="hello.html">Hello World</a>
<ul>
<li><a href="hello.html#makefile">Makefile</a></li>
<li><a href="hello.html#debug">Debug</a></li>
</ul>
</li>
<li><a href="elements.html">Elements</a>
<ul>
<li><a href="elements.html#ident">Identifiers</a></li>
<li><a href="elements.html#keywords">Keywords</a></li>
<li><a href="elements.html#const">Constants</a></li>
<li><a href="elements.html#op">Operators</a></li>
<li><a href="elements.html#sep">Separators</a></li>
<li><a href="elements.html#white">White space</a></li>
</ul>
</li>
<li><a href="datatypes.html">Datatypes</a>
<ul>
<li><a href="datatypes.html#types">Types</a></li>
<li><a href="datatypes.html#int">Integer</a></li>
<li><a href="datatypes.html#double">Real number</a></li>
<li><a href="datatypes.html#complex">Complex number</a></li>
<li><a href="datatypes.html#enum">Enumeration</a></li>
<li><a href="datatypes.html#union">Unions</a></li>
<li><a href="datatypes.html#struct">Structures</a></li>
<li><a href="datatypes.html#array">Arrays</a></li>
<li><a href="datatypes.html#pointer">Pointers</a></li>
<li><a href="datatypes.html#it">Incomplete types</a></li>
<li><a href="datatypes.html#tq">Type qualifiers</a></li>
<li><a href="datatypes.html#st">Storage class</a></li>
<li><a href="datatypes.html#format">Format type specifiers</a></li>
</ul>
</li>
<li><a href="">Operators & Expressions</a></li>
<li><a href="">Control Flow</a></li>
<li><a href="">Functions</a></li>
<li><a href="">Input & Output</a></li>
<li><a href="basic.html">Basic</a>
<ul>
<li><a href="basic.html#sources">Multiple sources</a></li>
</ul>
</li>
<li><a href="lib.html">Libraries</a>
<ul>
<li><a href="lib.html#basic">Basic libraries</a></li>
<li><a href="lib.html#advanced">Advanced libraries</a></li>
<li><a href="lib.html#random-numbers">Random Numbers</a></li>
<li><a href="lib.html#signals">Signals</a></li>
<li><a href="lib.html#sorting">Sorting</a></li>
<li><a href="lib.html#strings">Strings</a></li>
<li><a href="lib.html#inter-process-communication">Inter Process Communication</a></li>
<li><a href="lib.html#file-io">File IO</a></li>
<li><a href="lib.html#shared-memory">Shared Memory</a></li>
<li><a href="lib.html#networking">Networking</a></li>
<li><a href="lib.html#threads">Threads</a></li>
</ul>
</li>
<li><a href="c/debugging.html">Debugging</a></li>
<li><a href="c/system.html">System Development</a></li>
</ul>
<ul>
<li><a href="http://inti.sourceforge.net/tutorial/libinti/autotoolsproject.html">Autotools</a></li>
</ul>
<h1>C & GDB</h1>
<h2 id="hello">Hello World</h2>
<p>Create file hello.c with;</p>
<pre>
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
</pre>
<p>Compile;</p>
<pre>
$ gcc -Wall hello.c -o hello
</pre>
<p>Run;</p>
<pre>
$./hello
Hello World!
</pre>
<h2 ="sources">Multiple Sources</h2>
<p>To organize code in multiple files split above
example in main.c, hello.c and hello.h. Content of
main.c;<p>
<pre>
#include "hello.h"
int main() {
hello("world");
return 0;
}
</pre>
<p>Header file contains declaration of the function hello,
content of hello.h;</p>
<pre>
void hello(const char* name);
</pre>
<p>Implementation of hello function in hello.c;</p>
<pre>
#include <stdio.h>
#include "hello.h"
void hello(const char* name) {
printf("Hello, %s!\n", name);
}
</pre>
<p>Compile;</p>
<pre>
$ gcc -Wall main.c hello.c -o hello
</pre>
<h2 id="makefile">Makefile</h2>
<p>Make reads a Makefile by default on current directory,
Makefile defines targets, for example executables and their
dependencies, for example object files and source files.<p>
<p>Create Makefile;</p>
<pre>
CC=gcc
CFLAGS=-Wall
hello: main.o hello.o
clean:
rm -f hello main.o hello.o
</pre>
<pre>
$ touch NEWS README AUTHORS ChangeLog
</pre>
<h2 id="debug">Debug</h2>
<p>To use gdb you need to compile program with -g flag. Change
Makefile</p>
<pre>
CC=gcc
CFLAGS=-Wall -g
hello: main.o hello.o
clean:
rm -f hello main.o hello.o
</pre>
<pre>
$ gdb hello
</pre>
<p>Set break point;</p>
<pre>
(gdb) break main
</pre>
<p>To start the program you can type run, this way gdb
will try to run the program until the end. If program
crash, gdb will stop it for debuging. Start program;</p>
<pre>
(gdb) run
</pre>
<p>Step in next line;</p>
<pre>
(gdb) s
</pre>
<p>Print variable "name" value;</p>
<pre>
(gdb) print name
$1 = 0x4005b0 "world"
(gdb)
</pre>
<p>Print variable "name" type;</p>
<pre>
(gdb) ptype name
type = const char *
(gdb)
</pre>
<p>Variable is a <a href="elements.html#const">string constant</a>.
Execute next line to end;</p>
<pre>
(gdb) n
</pre>
<a href="../index.html">Development Index</a>
<p>
This is part of the Hive System Documentation.
Copyright (C) 2018
Hive Team.
See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
for copying conditions.</p>
</body>
</html>
|