5
5
<!-- but supporting the range of linking possibilities made available by other -->
6
6
<!-- languages is important for Rust to achieve seamless interaction with native -->
7
7
<!-- libraries. -->
8
-
9
8
Rustにおけるリンクの一般的なケースについては本書の前の方で説明しましたが、他言語から利用できるような幅広いリンクをサポートすることは、ネイティブライブラリーとのシームレスな相互利用を実現するために、Rustにとって重要です。
10
9
11
10
<!-- # Link args -->
@@ -15,7 +14,6 @@ Rustにおけるリンクの一般的なケースについては本書の前の
15
14
<!-- the `link_args` attribute. This attribute is applied to `extern` blocks and -->
16
15
<!-- specifies raw flags which need to get passed to the linker when producing an -->
17
16
<!-- artifact. An example usage would be: -->
18
-
19
17
どのようにリンクをカスタマイズするかを` rustc ` に指示するために、1つの方法があります。それは、` link_args ` 属性を使うことです。
20
18
この属性は` extern ` ブロックに適用され、生成物を作るときにリンカーに渡したいフラグをそのまま指定します。
21
19
使い方の例は次のようになります。
@@ -36,15 +34,13 @@ extern {}
36
34
<!-- LLVM directly to link native libraries, in which case `link_args` will have no -->
37
35
<!-- meaning. You can achieve the same effect as the `link_args` attribute with the -->
38
36
<!-- `-C link-args` argument to `rustc`. -->
39
-
40
37
これはリンクを実行するための認められた方法ではないため、この機能は現在` feature(link_args) ` ゲートによって隠されているということに注意しましょう。
41
38
今は` rustc ` がシステムリンカー(多くのシステムでは` gcc ` 、MSVCでは` link.exe ` )に渡すので、追加のコマンドライン引数を提供することには意味がありますが、それが今後もそうだとは限りません。
42
39
将来、` rustc ` がネイティブライブラリーをリンクするためにLLVMを直接使うようになるかもしれませんし、そのような場合には` link_args ` は意味がなくなるでしょう。
43
40
` rustc ` に` -C link-args ` 引数をつけることで、` link_args ` 属性と同じような効果を得ることができます。
44
41
45
42
<!-- It is highly recommended to *not* use this attribute, and rather use the more -->
46
43
<!-- formal `#[link(...)]` attribute on `extern` blocks instead. -->
47
-
48
44
この属性は使わ * ない* ことが強く推奨されているので、代わりにもっと正式な` #[link(...)] ` 属性を` extern ` ブロックに使いましょう。
49
45
50
46
<!-- # Static linking -->
@@ -57,15 +53,13 @@ extern {}
57
53
<!-- installing Rust everywhere. By contrast, native libraries -->
58
54
<!-- (e.g. `libc` and `libm`) are usually dynamically linked, but it is possible to -->
59
55
<!-- change this and statically link them as well. -->
60
-
61
56
スタティックリンクとは全ての必要なライブラリーを含めた出力を生成する手順のことで、そうすればコンパイルされたプロジェクトを使いたいシステム全てにライブラリーをインストールする必要がなくなります。
62
57
Rustのみで構築された依存関係はデフォルトでスタティックリンクされます。そのため、Rustをインストールしなくても、作成されたバイナリーやライブラリーを使うことができます。
63
58
対照的に、ネイティブライブラリー(例えば` libc ` や` libm ` )はダイナミックリンクされるのが普通です。しかし、これを変更してそれらを同様にスタティックリンクすることも可能です。
64
59
65
60
<!-- Linking is a very platform-dependent topic, and static linking may not even be -->
66
61
<!-- possible on some platforms! This section assumes some basic familiarity with -->
67
62
<!-- linking on your platform of choice. -->
68
-
69
63
リンクは非常にプラットフォームに依存した話題であり、スタティックリンクのできないプラットフォームすらあるかもしれません!
70
64
このセクションは選んだプラットフォームにおけるリンクについての基本が理解できていることを前提とします。
71
65
@@ -75,7 +69,6 @@ Rustのみで構築された依存関係はデフォルトでスタティック
75
69
<!-- By default, all Rust programs on Linux will link to the system `libc` along with -->
76
70
<!-- a number of other libraries. Let's look at an example on a 64-bit Linux machine -->
77
71
<!-- with GCC and `glibc` (by far the most common `libc` on Linux): -->
78
-
79
72
デフォルトでは、Linux上の全てのRustのプログラムはシステムの` libc ` とその他のいくつかのライブラリーとリンクされます。
80
73
GCCと` glibc ` (Linuxにおける最も一般的な` libc ` )を使った64ビットLinuxマシンでの例を見てみましょう。
81
74
@@ -97,13 +90,11 @@ $ ldd example
97
90
<!-- Dynamic linking on Linux can be undesirable if you wish to use new library -->
98
91
<!-- features on old systems or target systems which do not have the required -->
99
92
<!-- dependencies for your program to run. -->
100
-
101
93
古いシステムで新しいライブラリーの機能を使いたいときや、実行するプログラムに必要な依存関係を満たさないシステムをターゲットにしたいときは、Linuxにおけるダイナミックリンクは望ましくないかもしれません。
102
94
103
95
<!-- Static linking is supported via an alternative `libc`, [`musl`](http://www.musl-libc.org). You can compile -->
104
96
<!-- your own version of Rust with `musl` enabled and install it into a custom -->
105
97
<!-- directory with the instructions below: -->
106
-
107
98
スタティックリンクは代わりの` libc ` である[ ` musl ` ] ( http://www.musl-libc.org/ ) によってサポートされています。
108
99
以下の手順に従い、` musl ` を有効にした独自バージョンのRustをコンパイルして独自のディレクトリーにインストールすることができます。
109
100
@@ -151,7 +142,6 @@ $ du -h musldist/bin/rustc
151
142
<!-- You now have a build of a `musl`-enabled Rust! Because we've installed it to a -->
152
143
<!-- custom prefix we need to make sure our system can find the binaries and appropriate -->
153
144
<!-- libraries when we try and run it: -->
154
-
155
145
これで` musl ` が有効になったRustのビルドが手に入りました!
156
146
独自のプレフィックスを付けてインストールしたので、試しに実行するときにはシステムがバイナリーと適切なライブラリーを見付けられることを確かめなければなりません。
157
147
@@ -161,7 +151,6 @@ $ export LD_LIBRARY_PATH=$PREFIX/lib:$LD_LIBRARY_PATH
161
151
```
162
152
163
153
<!-- Let's try it out! -->
164
-
165
154
試してみましょう!
166
155
167
156
``` text
@@ -176,13 +165,11 @@ thread '<main>' panicked at 'failed', example.rs:1
176
165
177
166
<!-- Success! This binary can be copied to almost any Linux machine with the same -->
178
167
<!-- machine architecture and run without issues. -->
179
-
180
168
成功しました!
181
169
このバイナリーは同じマシンアーキテクチャーであればほとんど全てのLinuxマシンにコピーして問題なく実行することができます。
182
170
183
171
<!-- `cargo build` also permits the `--target` option so you should be able to build -->
184
172
<!-- your crates as normal. However, you may need to recompile your native libraries -->
185
173
<!-- against `musl` before they can be linked against. -->
186
-
187
174
` cargo build ` も` --target ` オプションを受け付けるので、あなたのクレートも普通にビルドできるはずです。
188
175
ただし、リンクする前にネイティブライブラリーを` musl ` 向けにリコンパイルする必要はあるかもしれません。
0 commit comments