Skip to content

Commit

Permalink
Fix #8: Use dollar with parentheses for command subsitutions since fi…
Browse files Browse the repository at this point in the history
…sh does not support backticks
  • Loading branch information
Evian-Zhang committed Nov 17, 2023
1 parent f383605 commit 4a7b03a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/14-汇编、链接与调试.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ as foo.s -o foo.o
链接:

```bash
ld foo.o -lSystem -L `xcrun --show-sdk-path -sdk macosx`/usr/lib -o foo
ld foo.o -lSystem -L $(xcrun --show-sdk-path -sdk macosx)/usr/lib -o foo
```

在汇编程序生成可执行程序的过程中,为什么需要分为汇编和链接两步呢?这两步分别是做什么的呢?
Expand Down Expand Up @@ -43,7 +43,7 @@ ar rcs lib14-foo-static.a 14-foo1.o 14-foo2.o
我们在`14-main.s`中使用了这个库,所以在把它翻译为`14-main.o`之后,我们要在链接时把这个库一块儿链接上:

```bash
ld -L. 14-main.o -l14-foo-static -lSystem -L `xcrun --show-sdk-path -sdk macosx`/usr/lib -o 14-main-static
ld -L. 14-main.o -l14-foo-static -lSystem -L $(xcrun --show-sdk-path -sdk macosx)/usr/lib -o 14-main-static
```

可以发现,它主要多了`-L.``-l14-foo-static`这两个选项。`-l14-foo-static`选项会使链接器在搜索路径下搜索`lib14-foo-static.a`文件,找到后一起链接;`-L.`则将当前目录添加到搜索路径下,使链接器可以找到我们刚刚生成的静态库。
Expand Down Expand Up @@ -77,15 +77,15 @@ otool -tvV 14-main-static
具体而言,我们得到目标文件`14-foo1.o``14-foo2.o`之后,将其链接为动态链接库:

```bash
ld 14-foo1.o 14-foo2.o -lSystem -L `xcrun --show-sdk-path -sdk macosx`/usr/lib -dylib -o lib14-foo-dynamic.dylib
ld 14-foo1.o 14-foo2.o -lSystem -L $(xcrun --show-sdk-path -sdk macosx)/usr/lib -dylib -o lib14-foo-dynamic.dylib
```

也就是说,在链接时增加`-dylib`选项。

在生成最终可执行程序时,和静态链接类似:

```bash
ld -L. 14-main.o -l14-foo-dynamic -lSystem -L `xcrun --show-sdk-path -sdk macosx`/usr/lib -o 14-main-dynamic
ld -L. 14-main.o -l14-foo-dynamic -lSystem -L $(xcrun --show-sdk-path -sdk macosx)/usr/lib -o 14-main-dynamic
```

同样是加`-L.``-l14-foo-dynamic`,这个`-l`选项会同时搜索`.a``.dylib`
Expand Down
2 changes: 1 addition & 1 deletion src/5-第一个汇编程序.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ as 5-basic.s -o 5-basic.o
然后再键入(关于这个指令为什么这么复杂,我在[macOS上使用链接器的正确姿势](https://gist.github.com/Evian-Zhang/19c63a1f1a1a58bdd4b86836a8b3ba0f)中详细论证了其必要性与正确性)

```bash
ld 5-basic.o -lSystem -L `xcrun --show-sdk-path -sdk macosx`/usr/lib -o 5-basic
ld 5-basic.o -lSystem -L $(xcrun --show-sdk-path -sdk macosx)/usr/lib -o 5-basic
```

此时该目录下应该会有一个叫`5-basic`的可执行文件,我们在终端下运行它:
Expand Down

0 comments on commit 4a7b03a

Please sign in to comment.