1
- % Advanced Linking
2
-
3
- The common cases of linking with Rust have been covered earlier in this book,
4
- but supporting the range of linking possibilities made available by other
5
- languages is important for Rust to achieve seamless interaction with native
6
- libraries.
7
-
8
- # Link args
9
-
10
- There is one other way to tell ` rustc ` how to customize linking, and that is via
11
- the ` link_args ` attribute. This attribute is applied to ` extern ` blocks and
12
- specifies raw flags which need to get passed to the linker when producing an
13
- artifact. An example usage would be:
1
+ % 高度なリンキング
2
+ <!-- % Advanced Linking -->
3
+
4
+ <!-- The common cases of linking with Rust have been covered earlier in this book, -->
5
+ <!-- but supporting the range of linking possibilities made available by other -->
6
+ <!-- languages is important for Rust to achieve seamless interaction with native -->
7
+ <!-- libraries. -->
8
+ Rustにおけるリンクの一般的なケースについては本書の前の方で説明しましたが、他言語から利用できるような幅広いリンクをサポートすることは、ネイティブライブラリとのシームレスな相互利用を実現するために、Rustにとって重要です。
9
+
10
+ <!-- # Link args -->
11
+ # リンク引数
12
+
13
+ <!-- There is one other way to tell `rustc` how to customize linking, and that is via -->
14
+ <!-- the `link_args` attribute. This attribute is applied to `extern` blocks and -->
15
+ <!-- specifies raw flags which need to get passed to the linker when producing an -->
16
+ <!-- artifact. An example usage would be: -->
17
+ どのようにリンクをカスタマイズするかを` rustc ` に指示するために、1つの方法があります。それは、` link_args ` アトリビュートを使うことです。
18
+ このアトリビュートは` extern ` ブロックに適用され、生成物を作るときにリンカに渡したいフラグをそのまま指定します。
19
+ 使い方の例は次のようになります。
14
20
15
21
``` no_run
16
22
#![feature(link_args)]
@@ -20,37 +26,51 @@ extern {}
20
26
# fn main() {}
21
27
```
22
28
23
- Note that this feature is currently hidden behind the ` feature(link_args) ` gate
24
- because this is not a sanctioned way of performing linking. Right now ` rustc `
25
- shells out to the system linker (` gcc ` on most systems, ` link.exe ` on MSVC),
26
- so it makes sense to provide extra command line
27
- arguments, but this will not always be the case. In the future ` rustc ` may use
28
- LLVM directly to link native libraries, in which case ` link_args ` will have no
29
- meaning. You can achieve the same effect as the ` link_args ` attribute with the
30
- ` -C link-args ` argument to ` rustc ` .
31
-
32
- It is highly recommended to * not* use this attribute, and rather use the more
33
- formal ` #[link(...)] ` attribute on ` extern ` blocks instead.
34
-
35
- # Static linking
36
-
37
- Static linking refers to the process of creating output that contains all
38
- required libraries and so doesn't need libraries installed on every system where
39
- you want to use your compiled project. Pure-Rust dependencies are statically
40
- linked by default so you can use created binaries and libraries without
41
- installing Rust everywhere. By contrast, native libraries
42
- (e.g. ` libc ` and ` libm ` ) are usually dynamically linked, but it is possible to
43
- change this and statically link them as well.
44
-
45
- Linking is a very platform-dependent topic, and static linking may not even be
46
- possible on some platforms! This section assumes some basic familiarity with
47
- linking on your platform of choice.
48
-
29
+ <!-- Note that this feature is currently hidden behind the `feature(link_args)` gate -->
30
+ <!-- because this is not a sanctioned way of performing linking. Right now `rustc` -->
31
+ <!-- shells out to the system linker (`gcc` on most systems, `link.exe` on MSVC), -->
32
+ <!-- so it makes sense to provide extra command line -->
33
+ <!-- arguments, but this will not always be the case. In the future `rustc` may use -->
34
+ <!-- LLVM directly to link native libraries, in which case `link_args` will have no -->
35
+ <!-- meaning. You can achieve the same effect as the `link_args` attribute with the -->
36
+ <!-- `-C link-args` argument to `rustc`. -->
37
+ これはリンクを実行するための認められた方法ではないため、この機能は現在` feature(link_args) ` ゲートによって隠されているということに注意しましょう。
38
+ 今は` rustc ` がシステムリンカ(多くのシステムでは` gcc ` 、MSVCでは` link.exe ` )に渡すので、追加のコマンドライン引数を提供することには意味がありますが、それが今後もそうだとは限りません。
39
+ 将来、` rustc ` がネイティブライブラリをリンクするためにLLVMを直接使うようになるかもしれませんし、そのような場合には` link_args ` は意味がなくなるでしょう。
40
+ ` rustc ` に` -C link-args ` 引数をつけることで、` link_args ` アトリビュートと同じような効果を得ることができます。
41
+
42
+ <!-- It is highly recommended to *not* use this attribute, and rather use the more -->
43
+ <!-- formal `#[link(...)]` attribute on `extern` blocks instead. -->
44
+ このアトリビュートは使わ * ない* ことが強く推奨されているので、代わりにもっと正式な` #[link(...)] ` アトリビュートを` extern ` ブロックに使いましょう。
45
+
46
+ <!-- # Static linking -->
47
+ # スタティックリンク
48
+
49
+ <!-- Static linking refers to the process of creating output that contains all -->
50
+ <!-- required libraries and so doesn't need libraries installed on every system where -->
51
+ <!-- you want to use your compiled project. Pure-Rust dependencies are statically -->
52
+ <!-- linked by default so you can use created binaries and libraries without -->
53
+ <!-- installing Rust everywhere. By contrast, native libraries -->
54
+ <!-- (e.g. `libc` and `libm`) are usually dynamically linked, but it is possible to -->
55
+ <!-- change this and statically link them as well. -->
56
+ スタティックリンクとは全ての必要なライブラリを含めた成果物を生成する手順のことで、そうすればコンパイルされたプロジェクトを使いたいシステム全てにライブラリをインストールする必要がなくなります。
57
+ Rustのみで構築された依存関係はデフォルトでスタティックリンクされます。そのため、Rustをインストールしなくても、作成されたバイナリやライブラリを使うことができます。
58
+ 対照的に、ネイティブライブラリ(例えば` libc ` や` libm ` )はダイナミックリンクされるのが普通です。しかし、これを変更してそれらを同様にスタティックリンクすることも可能です。
59
+
60
+ <!-- Linking is a very platform-dependent topic, and static linking may not even be -->
61
+ <!-- possible on some platforms! This section assumes some basic familiarity with -->
62
+ <!-- linking on your platform of choice. -->
63
+ リンクは非常にプラットフォームに依存した話題であり、スタティックリンクのできないプラットフォームすらあるかもしれません!
64
+ このセクションは選んだプラットフォームにおけるリンクについての基本が理解できていることを前提とします。
65
+
66
+ <!-- ## Linux -->
49
67
## Linux
50
68
51
- By default, all Rust programs on Linux will link to the system ` libc ` along with
52
- a number of other libraries. Let's look at an example on a 64-bit Linux machine
53
- with GCC and ` glibc ` (by far the most common ` libc ` on Linux):
69
+ <!-- By default, all Rust programs on Linux will link to the system `libc` along with -->
70
+ <!-- a number of other libraries. Let's look at an example on a 64-bit Linux machine -->
71
+ <!-- with GCC and `glibc` (by far the most common `libc` on Linux): -->
72
+ デフォルトでは、Linux上の全てのRustのプログラムはシステムの` libc ` とその他のいくつものライブラリとリンクされます。
73
+ GCCと` glibc ` (Linuxにおける最も一般的な` libc ` )を使った64ビットLinuxマシンでの例を見てみましょう。
54
74
55
75
``` text
56
76
$ cat example.rs
@@ -67,13 +87,16 @@ $ ldd example
67
87
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa817b93000)
68
88
```
69
89
70
- Dynamic linking on Linux can be undesirable if you wish to use new library
71
- features on old systems or target systems which do not have the required
72
- dependencies for your program to run.
90
+ <!-- Dynamic linking on Linux can be undesirable if you wish to use new library -->
91
+ <!-- features on old systems or target systems which do not have the required -->
92
+ <!-- dependencies for your program to run. -->
93
+ 古いシステムで新しいライブラリの機能を使いたいときや、実行するプログラムに必要な依存関係を満たさないシステムをターゲットにしたいときは、Linuxにおけるダイナミックリンクは望ましくないかもしれません。
73
94
74
- Static linking is supported via an alternative ` libc ` , [ ` musl ` ] ( http://www.musl-libc.org ) . You can compile
75
- your own version of Rust with ` musl ` enabled and install it into a custom
76
- directory with the instructions below:
95
+ <!-- Static linking is supported via an alternative `libc`, [`musl`](http://www.musl-libc.org). You can compile -->
96
+ <!-- your own version of Rust with `musl` enabled and install it into a custom -->
97
+ <!-- directory with the instructions below: -->
98
+ スタティックリンクは代わりの` libc ` である[ ` musl ` ] ( http://www.musl-libc.org/ ) によってサポートされています。
99
+ 以下の手順に従い、` musl ` を有効にした独自バージョンのRustをコンパイルして独自のディレクトリにインストールすることができます。
77
100
78
101
``` text
79
102
$ mkdir musldist
@@ -116,16 +139,19 @@ $ du -h musldist/bin/rustc
116
139
12K musldist/bin/rustc
117
140
```
118
141
119
- You now have a build of a ` musl ` -enabled Rust! Because we've installed it to a
120
- custom prefix we need to make sure our system can find the binaries and appropriate
121
- libraries when we try and run it:
142
+ <!-- You now have a build of a `musl`-enabled Rust! Because we've installed it to a -->
143
+ <!-- custom prefix we need to make sure our system can find the binaries and appropriate -->
144
+ <!-- libraries when we try and run it: -->
145
+ これで` musl ` が有効になったRustのビルドが手に入りました!
146
+ 独自のプレフィックスを付けてインストールしたので、試しに実行するときにはシステムがバイナリと適切なライブラリを見付けられることを確かめなければなりません。
122
147
123
148
``` text
124
149
$ export PATH=$PREFIX/bin:$PATH
125
150
$ export LD_LIBRARY_PATH=$PREFIX/lib:$LD_LIBRARY_PATH
126
151
```
127
152
128
- Let's try it out!
153
+ <!-- Let's try it out! -->
154
+ 試してみましょう!
129
155
130
156
``` text
131
157
$ echo 'fn main() { println!("hi!"); panic!("failed"); }' > example.rs
137
163
thread '<main>' panicked at 'failed', example.rs:1
138
164
```
139
165
140
- Success! This binary can be copied to almost any Linux machine with the same
141
- machine architecture and run without issues.
166
+ <!-- Success! This binary can be copied to almost any Linux machine with the same -->
167
+ <!-- machine architecture and run without issues. -->
168
+ 成功しました!
169
+ このバイナリは同じマシンアーキテクチャであればほとんど全てのLinuxマシンにコピーして問題なく実行することができます。
142
170
143
- ` cargo build ` also permits the ` --target ` option so you should be able to build
144
- your crates as normal. However, you may need to recompile your native libraries
145
- against ` musl ` before they can be linked against.
171
+ <!-- `cargo build` also permits the `--target` option so you should be able to build -->
172
+ <!-- your crates as normal. However, you may need to recompile your native libraries -->
173
+ <!-- against `musl` before they can be linked against. -->
174
+ ` cargo build ` も` --target ` オプションを受け付けるので、あなたのクレートも普通にビルドできるはずです。
175
+ ただし、リンクする前にネイティブライブラリを` musl ` 向けにリコンパイルする必要はあるかもしれません。
0 commit comments