@@ -122,6 +122,9 @@ specifies the kind of library with the following possible values:
122
122
- ` static ` — Indicates a static library.
123
123
- ` framework ` — Indicates a macOS framework. This is only valid for macOS
124
124
targets.
125
+ - ` raw-dylib ` — Indicates a dynamic library where the compiler will generate
126
+ an import library to link against (see [ ` dylib ` versus ` raw-dylib ` ] below
127
+ for details). This is only valid for Windows targets.
125
128
126
129
The ` name ` key must be included if ` kind ` is specified.
127
130
@@ -198,9 +201,26 @@ The default for this modifier is `-whole-archive`.
198
201
More implementation details about this modifier can be found in
199
202
[ ` whole-archive ` documentation for rustc] .
200
203
204
+ #### ` dylib ` versus ` raw-dylib `
205
+
206
+ On Windows, linking against a dynamic library requires that an import library
207
+ is provided to the linker: this is a special static library that declares all
208
+ of the symbols exported by the dynamic library in such a way that the linker
209
+ knows that they have to be dynamically loaded at runtime.
210
+
211
+ Specifying ` kind = "dylib" ` instructs the Rust compiler to link an import
212
+ library based on the ` name ` key. The linker will then use its normal library
213
+ resolution logic to find that import library. Alternatively, specifying
214
+ ` kind = "raw-dylib" ` instructs the compiler to generate an import library
215
+ during compilation and provide that to the linker instead.
216
+
217
+ ` raw-dylib ` is only supported on Windows and not supported on 32-bit x86
218
+ (` target_arch="x86" ` ). Using it when targeting other platforms or
219
+ x86 on Windows will result in a compiler error.
220
+
201
221
### The ` link_name ` attribute
202
222
203
- The ` link_name ` attribute may be specified on declarations inside an ` extern `
223
+ The * ` link_name ` attribute* may be specified on declarations inside an ` extern `
204
224
block to indicate the symbol to import for the given function or static. It
205
225
uses the [ _ MetaNameValueStr_ ] syntax to specify the name of the symbol.
206
226
@@ -211,6 +231,41 @@ extern {
211
231
}
212
232
```
213
233
234
+ Using this attribute with the ` link_ordinal ` attribute will result in a
235
+ compiler error.
236
+
237
+ ### The ` link_ordinal ` attribute
238
+
239
+ The * ` link_ordinal ` attribute* can be applied on declarations inside an ` extern `
240
+ block to indicate the numeric ordinal to use when generating the import library
241
+ to link against. An ordinal is a unique number per symbol exported by a dynamic
242
+ library on Windows and can be used when the library is being loaded to find
243
+ that symbol rather than having to look it up by name.
244
+
245
+ <div class =" warning " >
246
+
247
+ Warning: ` link_ordinal ` should only be used in cases where the ordinal of the
248
+ symbol is known to be stable: if the ordinal of a symbol is not explicitly set
249
+ when its containing binary is built then one will be automatically assigned to
250
+ it, and that assigned ordinal may change between builds of the binary.
251
+
252
+ </div >
253
+
254
+ <!-- ignore: Only works on x86 Windows -->
255
+ ``` rust,ignore
256
+ #[link(name = "exporter", kind = "raw-dylib")]
257
+ extern "stdcall" {
258
+ #[link_ordinal(15)]
259
+ fn imported_function_stdcall(i: i32);
260
+ }
261
+ ```
262
+
263
+ This attribute is only used with the ` raw-dylib ` linking kind.
264
+ Using any other kind will result in a compiler error.
265
+
266
+ Using this attribute with the ` link_name ` attribute will result in a
267
+ compiler error.
268
+
214
269
### Attributes on function parameters
215
270
216
271
Attributes on extern function parameters follow the same rules and
@@ -233,3 +288,4 @@ restrictions as [regular function parameters].
233
288
[ regular function parameters ] : functions.md#attributes-on-function-parameters
234
289
[ `bundle` documentation for rustc ] : ../../rustc/command-line-arguments.html#linking-modifiers-bundle
235
290
[ `whole-archive` documentation for rustc ] : ../../rustc/command-line-arguments.html#linking-modifiers-whole-archive
291
+ [ `dylib` versus `raw-dylib` ] : #dylib-versus-raw-dylib
0 commit comments