xref: /rust-libc-0.2.174/README.md (revision b46b6e2e)
1libc
2====
3
4Raw FFI bindings to platform libraries like `libc`.
5
6[![Build Status](https://travis-ci.org/rust-lang/libc.svg?branch=master)](https://travis-ci.org/rust-lang/libc)
7[![Build status](https://ci.appveyor.com/api/projects/status/github/rust-lang/libc?svg=true)](https://ci.appveyor.com/project/rust-lang-libs/libc)
8[![Latest version](https://img.shields.io/crates/v/libc.svg)](https://crates.io/crates/libc)
9[![Documentation](https://docs.rs/libc/badge.svg)](https://docs.rs/libc)
10![License](https://img.shields.io/crates/l/libc.svg)
11
12
13## Usage
14
15First, add the following to your `Cargo.toml`:
16
17```toml
18[dependencies]
19libc = "0.2"
20```
21
22Next, add this to your crate root:
23
24```rust
25extern crate libc;
26```
27
28Currently libc by default links to the standard library, but if you would
29instead like to use libc in a `#![no_std]` situation or crate you can request
30this via:
31
32```toml
33[dependencies]
34libc = { version = "0.2", default-features = false }
35```
36
37By default libc uses private fields in structs in order to enforce a certain
38memory alignment on them. These structs can be hard to instantiate outside of
39libc. To make libc use `#[repr(align(x))]`, instead of the private fields,
40activate the *align* feature. This requires Rust 1.25 or newer:
41
42```toml
43[dependencies]
44libc = { version = "0.2", features = ["align"] }
45```
46
47## What is libc?
48
49The primary purpose of this crate is to provide all of the definitions necessary
50to easily interoperate with C code (or "C-like" code) on each of the platforms
51that Rust supports. This includes type definitions (e.g. `c_int`), constants
52(e.g. `EINVAL`) as well as function headers (e.g. `malloc`).
53
54This crate does not strive to have any form of compatibility across platforms,
55but rather it is simply a straight binding to the system libraries on the
56platform in question.
57
58## Public API
59
60This crate exports all underlying platform types, functions, and constants under
61the crate root, so all items are accessible as `libc::foo`. The types and values
62of all the exported APIs match the platform that libc is compiled for.
63
64More detailed information about the design of this library can be found in its
65[associated RFC][rfc].
66
67[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1291-promote-libc.md
68
69## Adding an API
70
71Want to use an API which currently isn't bound in `libc`? It's quite easy to add
72one!
73
74The internal structure of this crate is designed to minimize the number of
75`#[cfg]` attributes in order to easily be able to add new items which apply
76to all platforms in the future. As a result, the crate is organized
77hierarchically based on platform. Each module has a number of `#[cfg]`'d
78children, but only one is ever actually compiled. Each module then reexports all
79the contents of its children.
80
81This means that for each platform that libc supports, the path from a
82leaf module to the root will contain all bindings for the platform in question.
83Consequently, this indicates where an API should be added! Adding an API at a
84particular level in the hierarchy means that it is supported on all the child
85platforms of that level. For example, when adding a Unix API it should be added
86to `src/unix/mod.rs`, but when adding a Linux-only API it should be added to
87`src/unix/notbsd/linux/mod.rs`.
88
89If you're not 100% sure at what level of the hierarchy an API should be added
90at, fear not! This crate has CI support which tests any binding against all
91platforms supported, so you'll see failures if an API is added at the wrong
92level or has different signatures across platforms.
93
94With that in mind, the steps for adding a new API are:
95
961. Determine where in the module hierarchy your API should be added.
972. Add the API.
983. Send a PR to this repo.
994. Wait for CI to pass, fixing errors.
1005. Wait for a merge!
101
102### Test before you commit
103
104We have two automated tests running on [Travis](https://travis-ci.org/rust-lang/libc):
105
1061. [`libc-test`](https://github.com/alexcrichton/ctest)
107  - `cd libc-test && cargo test`
108  - Use the `skip_*()` functions in `build.rs` if you really need a workaround.
1092. Style checker
110  - `rustc ci/style.rs && ./style src`
111
112### Releasing your change to crates.io
113
114Now that you've done the amazing job of landing your new API or your new
115platform in this crate, the next step is to get that sweet, sweet usage from
116crates.io! The only next step is to bump the version of libc and then publish
117it. If you'd like to get a release out ASAP you can follow these steps:
118
1191. Update the version number in `Cargo.toml`, you'll just be bumping the patch
120   version number.
1212. Run `cargo update` to regenerate the lockfile to encode your version bump in
122   the lock file. You may pull in some other updated dependencies, that's ok.
1233. Send a PR to this repository. It should [look like this][example], but it'd
124   also be nice to fill out the description with a small rationale for the
125   release (any rationale is ok though!)
1264. Once merged the release will be tagged and published by one of the libc crate
127   maintainers.
128
129[example]: https://github.com/rust-lang/libc/pull/583
130
131## Platforms and Documentation
132
133The following platforms are currently tested and have documentation available:
134
135Tested:
136  * [`i686-pc-windows-msvc`](https://rust-lang.github.io/libc/i686-pc-windows-msvc/libc/)
137  * [`x86_64-pc-windows-msvc`](https://rust-lang.github.io/libc/x86_64-pc-windows-msvc/libc/)
138    (Windows)
139  * [`i686-pc-windows-gnu`](https://rust-lang.github.io/libc/i686-pc-windows-gnu/libc/)
140  * [`x86_64-pc-windows-gnu`](https://rust-lang.github.io/libc/x86_64-pc-windows-gnu/libc/)
141  * [`i686-apple-darwin`](https://rust-lang.github.io/libc/i686-apple-darwin/libc/)
142  * [`x86_64-apple-darwin`](https://rust-lang.github.io/libc/x86_64-apple-darwin/libc/)
143    (OSX)
144  * `i386-apple-ios`
145  * `x86_64-apple-ios`
146  * [`i686-unknown-linux-gnu`](https://rust-lang.github.io/libc/i686-unknown-linux-gnu/libc/)
147  * [`x86_64-unknown-linux-gnu`](https://rust-lang.github.io/libc/x86_64-unknown-linux-gnu/libc/)
148    (Linux)
149  * [`x86_64-unknown-linux-musl`](https://rust-lang.github.io/libc/x86_64-unknown-linux-musl/libc/)
150    (Linux MUSL)
151  * [`aarch64-unknown-linux-gnu`](https://rust-lang.github.io/libc/aarch64-unknown-linux-gnu/libc/)
152    (Linux)
153  * `aarch64-unknown-linux-musl`
154    (Linux MUSL)
155  * [`sparc64-unknown-linux-gnu`](https://rust-lang.github.io/libc/sparc64-unknown-linux-gnu/libc/)
156    (Linux)
157  * [`mips-unknown-linux-gnu`](https://rust-lang.github.io/libc/mips-unknown-linux-gnu/libc/)
158  * [`arm-unknown-linux-gnueabihf`](https://rust-lang.github.io/libc/arm-unknown-linux-gnueabihf/libc/)
159  * [`arm-linux-androideabi`](https://rust-lang.github.io/libc/arm-linux-androideabi/libc/)
160    (Android)
161  * [`x86_64-unknown-freebsd`](https://rust-lang.github.io/libc/x86_64-unknown-freebsd/libc/)
162  * [`x86_64-unknown-openbsd`](https://rust-lang.github.io/libc/x86_64-unknown-openbsd/libc/)
163  * [`x86_64-rumprun-netbsd`](https://rust-lang.github.io/libc/x86_64-unknown-netbsd/libc/)
164
165The following may be supported, but are not guaranteed to always work:
166
167  * `i686-unknown-freebsd`
168  * [`x86_64-unknown-bitrig`](https://rust-lang.github.io/libc/x86_64-unknown-bitrig/libc/)
169  * [`x86_64-unknown-dragonfly`](https://rust-lang.github.io/libc/x86_64-unknown-dragonfly/libc/)
170  * `i686-unknown-haiku`
171  * `x86_64-unknown-haiku`
172  * [`x86_64-unknown-netbsd`](https://rust-lang.github.io/libc/x86_64-unknown-netbsd/libc/)
173  * [`x86_64-sun-solaris`](https://rust-lang.github.io/libc/x86_64-sun-solaris/libc/)
174