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