Skip to content

Commit f0b4390

Browse files
committed
fix formatting
1 parent 91f6b4a commit f0b4390

File tree

2 files changed

+40
-101
lines changed

2 files changed

+40
-101
lines changed

_practice/2023-11-16.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
1. Compare them in terms of time (assume that each gate requires one unite of time)
66
1. Compare them in terms of space/cost by counting the total number of gates required.
77
```
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 (`&, |, ^, >>, <<, ~`)):
99
```
1010
4 (_) 128
1111
12493 (_) -12494

notes/2023-11-07.md

Lines changed: 39 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,31 @@ kernelspec:
66

77
# Linking and Object file binary representation
88

9-
```{important}
10-
I am behind on this, filling in this using notes from previous semesters and making a PR (only the first, not a duplicate) can be an explore badge.
11-
```
12-
13-
14-
```{code-cell} bash
15-
:tags: ["skip-execution"]
16-
ls
17-
```
9+
## Why is the linking step important?
1810

19-
```{code-block} console
20-
fa23-kwl-brownsarahm ghic test
21-
fall2023 github-inclass-fa23-brownsarahm tiny-book
22-
```
11+
Let's make an empty folder to work with. I made my in my `inclass/sytems` folder.
2312

2413

2514
```{code-cell} bash
2615
:tags: ["skip-execution"]
2716
mkdir cexample
2817
```
2918

30-
```{code-block} console
31-
```
32-
19+
and then go in that new folder
3320

3421
```{code-cell} bash
3522
:tags: ["skip-execution"]
3623
cd cexample/
3724
```
3825

39-
```{code-block} console
40-
```
41-
26+
We are going to make two files, first
4227

4328
```{code-cell} bash
4429
:tags: ["skip-execution"]
4530
nano main.c
4631
```
4732

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:
5634

5735
```C
5836
/* Used to illustrate separate compilation.
@@ -72,19 +50,14 @@ void main () {
7250
```
7351

7452

53+
And then make a second file:
54+
7555
```{code-cell} bash
7656
:tags: ["skip-execution"]
7757
nano help.c
7858
```
7959

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:
8861

8962
```C
9063
/* Used to illustrate separate compilation
@@ -118,12 +91,15 @@ int product (int n) {
11891
}
11992
```
12093
94+
First we can make the two objects:
12195
12296
```{code-cell} bash
12397
:tags: ["skip-execution"]
12498
gcc -Wall -g -c main.c
12599
```
126100

101+
but here we get an error:
102+
127103
```{code-block} console
128104
main.c:8:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
129105
void main () {
@@ -144,31 +120,20 @@ main.c:13:56: error: implicit declaration of function 'product' is invalid in C9
144120
```
145121

146122

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+
int sum(int n);
126+
int product (int n);
154127
```
155-
128+
to the `main.c`
156129
157130
```{code-cell} bash
158131
:tags: ["skip-execution"]
159132
nano main.c
160133
```
161134

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
172137
/* Used to illustrate separate compilation.
173138
Created: Joe Zachary, October 22, 1992
174139
Modified:
@@ -187,7 +152,7 @@ void main () {
187152
}
188153
```
189154
190-
155+
Now we can make this object file
191156
```{code-cell} bash
192157
:tags: ["skip-execution"]
193158
gcc -Wall -g -c main.c
@@ -203,7 +168,7 @@ void main () {
203168
int
204169
1 warning generated.
205170
```
206-
171+
we get a warning, but that's okay
207172

208173
```{code-cell} bash
209174
:tags: ["skip-execution"]
@@ -214,6 +179,7 @@ ls
214179
help.c main.c main.o
215180
```
216181

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.
217183

218184
```{code-cell} bash
219185
:tags: ["skip-execution"]
@@ -230,34 +196,22 @@ ld: symbol(s) not found for architecture x86_64
230196
clang: error: linker command failed with exit code 1 (use -v to see invocation)
231197
```
232198

199+
So, we make the object file for the helper functions
233200

234201
```{code-cell} bash
235202
:tags: ["skip-execution"]
236203
gcc -Wall -g -c help.c
237204
```
238205

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-
```
251206

207+
Now, we can finally link and make it executble:
252208

253209
```{code-cell} bash
254210
:tags: ["skip-execution"]
255211
gcc -o demo main.o help.o -lm
256212
```
257213

258-
```{code-block} console
259-
```
260-
214+
and we can run our program
261215

262216
```{code-cell} bash
263217
:tags: ["skip-execution"]
@@ -271,31 +225,21 @@ The product of the first n integers is 120
271225
```
272226

273227

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+
````
278231

279-
```{code-block} console
280-
-bash: 8: command not found
281-
```
232+
If we edit,
282233

283234

284235
```{code-cell} bash
285236
:tags: ["skip-execution"]
286237
nano main.c
287238
```
288239

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:
297241

298-
```{code-block} console
242+
```C
299243
/* Used to illustrate separate compilation.
300244
Created: Joe Zachary, October 22, 1992
301245
Modified:
@@ -314,6 +258,10 @@ void main () {
314258
}
315259
```
316260
261+
We added an `!` so it is just different.
262+
263+
If we run again, it is not different.
264+
317265
318266
```{code-cell} bash
319267
:tags: ["skip-execution"]
@@ -326,6 +274,7 @@ The sum of the first n integers is 28
326274
The product of the first n integers is 5040
327275
```
328276

277+
Now, we need to recompile the main file.
329278

330279
```{code-cell} bash
331280
:tags: ["skip-execution"]
@@ -343,15 +292,14 @@ int
343292
1 warning generated.
344293
```
345294

295+
and then relink them into an executable
346296

347297
```{code-cell} bash
348298
:tags: ["skip-execution"]
349299
gcc -o demo main.o help.o -lm
350300
```
351301

352-
```{code-block} console
353-
```
354-
302+
for the changes to apply
355303

356304
```{code-cell} bash
357305
:tags: ["skip-execution"]
@@ -364,6 +312,7 @@ The sum of the first n integers is 36
364312
The product of the first n integers is 40320
365313
```
366314

315+
We see that this makes the executble and the two object files
367316

368317
```{code-cell} bash
369318
:tags: ["skip-execution"]
@@ -381,24 +330,14 @@ drwxr-xr-x 9 brownsarahm staff 288 Nov 7 12:37 ..
381330
-rw-r--r-- 1 brownsarahm staff 2400 Nov 7 12:53 main.o
382331
```
383332

384-
385-
```{code-cell} bash
386-
:tags: ["skip-execution"]
387-
ls
388-
```
389-
390-
```{code-block} console
391-
demo help.c help.o main.c main.o
392-
```
333+
We can change the name of the executable with the `-o` option.
393334

394335

395336
```{code-cell} bash
396337
:tags: ["skip-execution"]
397338
gcc -o sumProd main.o help.o -lm
398339
```
399340

400-
```{code-block} console
401-
```
402341

403342

404343
```{code-cell} bash

0 commit comments

Comments
 (0)