• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-5,2503,798

tests/H03-May-2022-739591

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitignoreH A D29-Nov-197330 54

Cargo.lockH A D01-Jan-19703.9 KiB156137

Cargo.tomlH A D01-Jan-19701.2 KiB3431

Cargo.toml.orig-cargoH A D29-Nov-1973767 2824

LICENSE-APACHEH A D29-Nov-197310.6 KiB202169

LICENSE-MITH A D29-Nov-19731 KiB2622

README.mdH A D29-Nov-19737.7 KiB224171

README.md

1# cc-rs
2
3A library to compile C/C++/assembly into a Rust library/application.
4
5[Documentation](https://docs.rs/cc)
6
7A simple library meant to be used as a build dependency with Cargo packages in
8order to build a set of C/C++ files into a static archive. This crate calls out
9to the most relevant compiler for a platform, for example using `cl` on MSVC.
10
11## Using cc-rs
12
13First, you'll want to both add a build script for your crate (`build.rs`) and
14also add this crate to your `Cargo.toml` via:
15
16```toml
17[build-dependencies]
18cc = "1.0"
19```
20
21Next up, you'll want to write a build script like so:
22
23```rust,no_run
24// build.rs
25
26fn main() {
27    cc::Build::new()
28        .file("foo.c")
29        .file("bar.c")
30        .compile("foo");
31}
32```
33
34And that's it! Running `cargo build` should take care of the rest and your Rust
35application will now have the C files `foo.c` and `bar.c` compiled into a file
36named `libfoo.a`. If the C files contain
37
38```c
39void foo_function(void) { ... }
40```
41
42and
43
44```c
45int32_t bar_function(int32_t x) { ... }
46```
47
48you can call them from Rust by declaring them in
49your Rust code like so:
50
51```rust,no_run
52extern {
53    fn foo_function();
54    fn bar_function(x: i32) -> i32;
55}
56
57pub fn call() {
58    unsafe {
59        foo_function();
60        bar_function(42);
61    }
62}
63
64fn main() {
65    // ...
66}
67```
68
69See [the Rustonomicon](https://doc.rust-lang.org/nomicon/ffi.html) for more details.
70
71## External configuration via environment variables
72
73To control the programs and flags used for building, the builder can set a
74number of different environment variables.
75
76* `CFLAGS` - a series of space separated flags passed to compilers. Note that
77             individual flags cannot currently contain spaces, so doing
78             something like: `-L=foo\ bar` is not possible.
79* `CC` - the actual C compiler used. Note that this is used as an exact
80         executable name, so (for example) no extra flags can be passed inside
81         this variable, and the builder must ensure that there aren't any
82         trailing spaces. This compiler must understand the `-c` flag. For
83         certain `TARGET`s, it also is assumed to know about other flags (most
84         common is `-fPIC`).
85* `AR` - the `ar` (archiver) executable to use to build the static library.
86* `CRATE_CC_NO_DEFAULTS` - the default compiler flags may cause conflicts in some cross compiling scenarios. Setting this variable will disable the generation of default compiler flags.
87* `CXX...` - see [C++ Support](#c-support).
88
89Each of these variables can also be supplied with certain prefixes and suffixes,
90in the following prioritized order:
91
921. `<var>_<target>` - for example, `CC_x86_64-unknown-linux-gnu`
932. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
943. `<build-kind>_<var>` - for example, `HOST_CC` or `TARGET_CFLAGS`
954. `<var>` - a plain `CC`, `AR` as above.
96
97If none of these variables exist, cc-rs uses built-in defaults
98
99In addition to the above optional environment variables, `cc-rs` has some
100functions with hard requirements on some variables supplied by [cargo's
101build-script driver][cargo] that it has the `TARGET`, `OUT_DIR`, `OPT_LEVEL`,
102and `HOST` variables.
103
104[cargo]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#inputs-to-the-build-script
105
106## Optional features
107
108### Parallel
109
110Currently cc-rs supports parallel compilation (think `make -jN`) but this
111feature is turned off by default. To enable cc-rs to compile C/C++ in parallel,
112you can change your dependency to:
113
114```toml
115[build-dependencies]
116cc = { version = "1.0", features = ["parallel"] }
117```
118
119By default cc-rs will limit parallelism to `$NUM_JOBS`, or if not present it
120will limit it to the number of cpus on the machine. If you are using cargo,
121use `-jN` option of `build`, `test` and `run` commands as `$NUM_JOBS`
122is supplied by cargo.
123
124## Compile-time Requirements
125
126To work properly this crate needs access to a C compiler when the build script
127is being run. This crate does not ship a C compiler with it. The compiler
128required varies per platform, but there are three broad categories:
129
130* Unix platforms require `cc` to be the C compiler. This can be found by
131  installing cc/clang on Linux distributions and Xcode on macOS, for example.
132* Windows platforms targeting MSVC (e.g. your target triple ends in `-msvc`)
133  require `cl.exe` to be available and in `PATH`. This is typically found in
134  standard Visual Studio installations and the `PATH` can be set up by running
135  the appropriate developer tools shell.
136* Windows platforms targeting MinGW (e.g. your target triple ends in `-gnu`)
137  require `cc` to be available in `PATH`. We recommend the
138  [MinGW-w64](http://mingw-w64.org) distribution, which is using the
139  [Win-builds](http://win-builds.org) installation system.
140  You may also acquire it via
141  [MSYS2](https://www.msys2.org/), as explained [here][msys2-help].  Make sure
142  to install the appropriate architecture corresponding to your installation of
143  rustc. GCC from older [MinGW](http://www.mingw.org) project is compatible
144  only with 32-bit rust compiler.
145
146[msys2-help]: https://github.com/rust-lang/rust#building-on-windows
147
148## C++ support
149
150`cc-rs` supports C++ libraries compilation by using the `cpp` method on
151`Build`:
152
153```rust,no_run
154fn main() {
155    cc::Build::new()
156        .cpp(true) // Switch to C++ library compilation.
157        .file("foo.cpp")
158        .compile("libfoo.a");
159}
160```
161
162For C++ libraries, the `CXX` and `CXXFLAGS` environment variables are used instead of `CC` and `CFLAGS`.
163
164The C++ standard library may be linked to the crate target. By default it's `libc++` for macOS, FreeBSD, and OpenBSD, `libc++_shared` for Android, nothing for MSVC, and `libstdc++` for anything else. It can be changed in one of two ways:
165
1661. by using the `cpp_link_stdlib` method on `Build`:
167    ```rust,no-run
168    fn main() {
169        cc::Build::new()
170            .cpp(true)
171            .file("foo.cpp")
172            .cpp_link_stdlib("stdc++") // use libstdc++
173            .compile("libfoo.a");
174    }
175    ```
1762. by setting the `CXXSTDLIB` environment variable.
177
178In particular, for Android you may want to [use `c++_static` if you have at most one shared library](https://developer.android.com/ndk/guides/cpp-support).
179
180Remember that C++ does name mangling so `extern "C"` might be required to enable Rust linker to find your functions.
181
182## CUDA C++ support
183
184`cc-rs` also supports compiling CUDA C++ libraries by using the `cuda` method
185on `Build` (currently for GNU/Clang toolchains only):
186
187```rust,no_run
188fn main() {
189    cc::Build::new()
190        // Switch to CUDA C++ library compilation using NVCC.
191        .cuda(true)
192        .cudart("static")
193        // Generate code for Maxwell (GTX 970, 980, 980 Ti, Titan X).
194        .flag("-gencode").flag("arch=compute_52,code=sm_52")
195        // Generate code for Maxwell (Jetson TX1).
196        .flag("-gencode").flag("arch=compute_53,code=sm_53")
197        // Generate code for Pascal (GTX 1070, 1080, 1080 Ti, Titan Xp).
198        .flag("-gencode").flag("arch=compute_61,code=sm_61")
199        // Generate code for Pascal (Tesla P100).
200        .flag("-gencode").flag("arch=compute_60,code=sm_60")
201        // Generate code for Pascal (Jetson TX2).
202        .flag("-gencode").flag("arch=compute_62,code=sm_62")
203        .file("bar.cu")
204        .compile("libbar.a");
205}
206```
207
208## License
209
210This project is licensed under either of
211
212 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
213   https://www.apache.org/licenses/LICENSE-2.0)
214 * MIT license ([LICENSE-MIT](LICENSE-MIT) or
215   https://opensource.org/licenses/MIT)
216
217at your option.
218
219### Contribution
220
221Unless you explicitly state otherwise, any contribution intentionally submitted
222for inclusion in cc-rs by you, as defined in the Apache-2.0 license, shall be
223dual licensed as above, without any additional terms or conditions.
224