Skip to content

Commit 8844fd4

Browse files
committed
Adding sections and brief explanation of libc
Have included an example of a Cargo.toml file containing the libc dependency. The file has been reformatted to use sections. The note on the default features warning is part of the _Using libc_ section but is in bold.
1 parent 299a1a0 commit 8844fd4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/doc/book/no-stdlib.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,26 @@ don’t want to use the standard library via an attribute: `#![no_std]`.
1212
> `#![no_std]`](using-rust-without-the-standard-library.html)
1313
1414
Obviously there's more to life than just libraries: one can use
15-
`#[no_std]` with an executable, controlling the entry point is
16-
possible in two ways: the `#[start]` attribute, or overriding the
17-
default shim for the C `main` function with your own.
15+
`#[no_std]` with an executable.
16+
17+
### Using libc
18+
19+
In order to build a `#[no_std]` executable we will need libc as a dependency. We can specify
20+
this using our `Cargo.toml` file:
21+
22+
```toml
23+
[dependencies]
24+
libc = { version = "0.2.11", default-features = false }
25+
```
26+
27+
Note that the default features have been disabled. This is a critical step -
28+
**the default features of libc include the standard library and so must be
29+
disabled.**
30+
31+
### Writing an executable without stdlib
32+
33+
Controlling the entry point is possible in two ways: the `#[start]` attribute,
34+
or overriding the default shim for the C `main` function with your own.
1835

1936
The function marked `#[start]` is passed the command line parameters
2037
in the same format as C:
@@ -45,9 +62,6 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
4562
# // fn main() {} tricked you, rustdoc!
4663
```
4764

48-
> Note: Ensure that you are not including the default features with libc. Otherwise you will
49-
> implicitly use libstd.
50-
5165
To override the compiler-inserted `main` shim, one has to disable it
5266
with `#![no_main]` and then create the appropriate symbol with the
5367
correct ABI and the correct name, which requires overriding the
@@ -75,7 +89,6 @@ pub extern fn main(argc: i32, argv: *const *const u8) -> i32 {
7589
# // fn main() {} tricked you, rustdoc!
7690
```
7791

78-
7992
The compiler currently makes a few assumptions about symbols which are available
8093
in the executable to call. Normally these functions are provided by the standard
8194
library, but without it you must define your own.

0 commit comments

Comments
 (0)