You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _practice/2023-11-16.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@
5
5
1. Compare them in terms of time (assume that each gate requires one unite of time)
6
6
1. Compare them in terms of space/cost by counting the total number of gates required.
7
7
```
8
-
3. Add {index}`bitwise.md` to your kwl and write the bitwise operations required for the following transformations (replace the `(_)` with a bitwise operator (`&, |, ^, >>, <<`)):
8
+
3. Add {index}`bitwise.md` to your kwl and write the bitwise operations required for the following transformations (replace the `(_)` with a bitwise operator (`&, |, ^, >>, <<, ~`)):
Let's make an empty folder to work with. I made my in my `inclass/sytems` folder.
23
12
24
13
25
14
```{code-cell} bash
26
15
:tags: ["skip-execution"]
27
16
mkdir cexample
28
17
```
29
18
30
-
```{code-block} console
31
-
```
32
-
19
+
and then go in that new folder
33
20
34
21
```{code-cell} bash
35
22
:tags: ["skip-execution"]
36
23
cd cexample/
37
24
```
38
25
39
-
```{code-block} console
40
-
```
41
-
26
+
We are going to make two files, first
42
27
43
28
```{code-cell} bash
44
29
:tags: ["skip-execution"]
45
30
nano main.c
46
31
```
47
32
48
-
```{code-block} console
49
-
```
50
-
51
-
52
-
```{code-cell} bash
53
-
:tags: ["skip-execution"]
54
-
cat main.c
55
-
```
33
+
and put this content in the file:
56
34
57
35
```C
58
36
/* Used to illustrate separate compilation.
@@ -72,19 +50,14 @@ void main () {
72
50
```
73
51
74
52
53
+
And then make a second file:
54
+
75
55
```{code-cell} bash
76
56
:tags: ["skip-execution"]
77
57
nano help.c
78
58
```
79
59
80
-
```{code-block} console
81
-
```
82
-
83
-
84
-
```{code-cell} bash
85
-
:tags: ["skip-execution"]
86
-
cat help.c
87
-
```
60
+
with the contents that describes two functions:
88
61
89
62
```C
90
63
/* Used to illustrate separate compilation
@@ -118,12 +91,15 @@ int product (int n) {
118
91
}
119
92
```
120
93
94
+
First we can make the two objects:
121
95
122
96
```{code-cell} bash
123
97
:tags: ["skip-execution"]
124
98
gcc -Wall -g -c main.c
125
99
```
126
100
101
+
but here we get an error:
102
+
127
103
```{code-block} console
128
104
main.c:8:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
129
105
void main () {
@@ -144,31 +120,20 @@ main.c:13:56: error: implicit declaration of function 'product' is invalid in C9
144
120
```
145
121
146
122
147
-
```{code-cell} bash
148
-
:tags: ["skip-execution"]
149
-
man gcc
150
-
```
151
-
152
-
```{code-block} console
153
-
No manual entry for gcc
123
+
We can get around this, by telling main about the functions by adding
124
+
```C
125
+
intsum(int n);
126
+
int product (int n);
154
127
```
155
-
128
+
to the `main.c`
156
129
157
130
```{code-cell} bash
158
131
:tags: ["skip-execution"]
159
132
nano main.c
160
133
```
161
134
162
-
```{code-block} console
163
-
```
164
-
165
-
166
-
```{code-cell} bash
167
-
:tags: ["skip-execution"]
168
-
cat main.c
169
-
```
170
-
171
-
```{code-block} console
135
+
so that it is like this:
136
+
```C
172
137
/* Used to illustrate separate compilation.
173
138
Created: Joe Zachary, October 22, 1992
174
139
Modified:
@@ -187,7 +152,7 @@ void main () {
187
152
}
188
153
```
189
154
190
-
155
+
Now we can make this object file
191
156
```{code-cell} bash
192
157
:tags: ["skip-execution"]
193
158
gcc -Wall -g -c main.c
@@ -203,7 +168,7 @@ void main () {
203
168
int
204
169
1 warning generated.
205
170
```
206
-
171
+
we get a warning, but that's okay
207
172
208
173
```{code-cell} bash
209
174
:tags: ["skip-execution"]
@@ -214,6 +179,7 @@ ls
214
179
help.c main.c main.o
215
180
```
216
181
182
+
If we try to make this an executable, we cannot because then it looks for what those functions are supposed to do, but it cannot do that yet.
217
183
218
184
```{code-cell} bash
219
185
:tags: ["skip-execution"]
@@ -230,34 +196,22 @@ ld: symbol(s) not found for architecture x86_64
230
196
clang: error: linker command failed with exit code 1 (use -v to see invocation)
231
197
```
232
198
199
+
So, we make the object file for the helper functions
233
200
234
201
```{code-cell} bash
235
202
:tags: ["skip-execution"]
236
203
gcc -Wall -g -c help.c
237
204
```
238
205
239
-
```{code-block} console
240
-
```
241
-
242
-
243
-
```{code-cell} bash
244
-
:tags: ["skip-execution"]
245
-
ls
246
-
```
247
-
248
-
```{code-block} console
249
-
help.c help.o main.c main.o
250
-
```
251
206
207
+
Now, we can finally link and make it executble:
252
208
253
209
```{code-cell} bash
254
210
:tags: ["skip-execution"]
255
211
gcc -o demo main.o help.o -lm
256
212
```
257
213
258
-
```{code-block} console
259
-
```
260
-
214
+
and we can run our program
261
215
262
216
```{code-cell} bash
263
217
:tags: ["skip-execution"]
@@ -271,31 +225,21 @@ The product of the first n integers is 120
271
225
```
272
226
273
227
274
-
```{code-cell} bash
275
-
:tags: ["skip-execution"]
276
-
8
277
-
```
228
+
````{tip}
229
+
One reason we split code is to make it readable, but another reason is what we just did. We can compile each file separately, when your code is large and compiling takes a long time, splitting it will mean you only have to recompile the file(s) you have recently changed and relink, instead of recompiling everything.
230
+
````
278
231
279
-
```{code-block} console
280
-
-bash: 8: command not found
281
-
```
232
+
If we edit,
282
233
283
234
284
235
```{code-cell} bash
285
236
:tags: ["skip-execution"]
286
237
nano main.c
287
238
```
288
239
289
-
```{code-block} console
290
-
```
291
-
292
-
293
-
```{code-cell} bash
294
-
:tags: ["skip-execution"]
295
-
cat main.c
296
-
```
240
+
to look like the following:
297
241
298
-
```{code-block} console
242
+
```C
299
243
/* Used to illustrate separate compilation.
300
244
Created: Joe Zachary, October 22, 1992
301
245
Modified:
@@ -314,6 +258,10 @@ void main () {
314
258
}
315
259
```
316
260
261
+
We added an `!` so it is just different.
262
+
263
+
If we run again, it is not different.
264
+
317
265
318
266
```{code-cell} bash
319
267
:tags: ["skip-execution"]
@@ -326,6 +274,7 @@ The sum of the first n integers is 28
326
274
The product of the first n integers is 5040
327
275
```
328
276
277
+
Now, we need to recompile the main file.
329
278
330
279
```{code-cell} bash
331
280
:tags: ["skip-execution"]
@@ -343,15 +292,14 @@ int
343
292
1 warning generated.
344
293
```
345
294
295
+
and then relink them into an executable
346
296
347
297
```{code-cell} bash
348
298
:tags: ["skip-execution"]
349
299
gcc -o demo main.o help.o -lm
350
300
```
351
301
352
-
```{code-block} console
353
-
```
354
-
302
+
for the changes to apply
355
303
356
304
```{code-cell} bash
357
305
:tags: ["skip-execution"]
@@ -364,6 +312,7 @@ The sum of the first n integers is 36
364
312
The product of the first n integers is 40320
365
313
```
366
314
315
+
We see that this makes the executble and the two object files
0 commit comments