Expand description
A library for build scripts to compile custom C code
This library is intended to be used as a build-dependencies
entry in
Cargo.toml
:
[build-dependencies]
cc = "1.0"
The purpose of this crate is to provide the utility functions necessary to
compile C code into a static archive which is then linked into a Rust crate.
Configuration is available through the Build
struct.
This crate will automatically detect situations such as cross compilation or other environment variables set by Cargo and will build code appropriately.
The crate is not limited to C code, it can accept any source code that can
be passed to a C or C++ compiler. As such, assembly files with extensions
.s
(gcc/clang) and .asm
(MSVC) can also be compiled.
Parallelism
To parallelize computation, enable the parallel
feature for the crate.
[build-dependencies]
cc = { version = "1.0", features = ["parallel"] }
To specify the max number of concurrent compilation jobs, set the NUM_JOBS
environment variable to the desired amount.
Cargo will also set this environment variable when executed with the -jN
flag.
If NUM_JOBS
is not set, the RAYON_NUM_THREADS
environment variable can
also specify the build paralellism.
Examples
Use the Build
struct to compile src/foo.c
:
extern crate cc;
fn main() {
cc::Build::new()
.file("src/foo.c")
.define("FOO", Some("bar"))
.include("src")
.compile("foo");
}