3 A library to compile C/C++/assembly into a Rust library/application.
5 [![Build Status](https://travis-ci.org/alexcrichton/cc-rs.svg?branch=master)](https://travis-ci.org/alexcrichton/cc-rs)
6 [![Build status](https://ci.appveyor.com/api/projects/status/onu270iw98h81nwv?svg=true)](https://ci.appveyor.com/project/alexcrichton/cc-rs)
8 [Documentation](https://docs.rs/cc)
10 A simple library meant to be used as a build dependency with Cargo packages in
11 order to build a set of C/C++ files into a static archive. This crate calls out
12 to the most relevant compiler for a platform, for example using `cl` on MSVC.
14 > **Note**: this crate was recently renamed from the `gcc` crate, so if you're
15 > looking for the `gcc` crate you're in the right spot!
19 First, you'll want to both add a build script for your crate (`build.rs`) and
20 also add this crate to your `Cargo.toml` via:
27 Next up, you'll want to write a build script like so:
42 And that's it! Running `cargo build` should take care of the rest and your Rust
43 application will now have the C files `foo.c` and `bar.c` compiled into a file
44 named libfoo.a. You can call the functions in Rust by declaring functions in
45 your Rust code like so:
65 ## External configuration via environment variables
67 To control the programs and flags used for building, the builder can set a
68 number of different environment variables.
70 * `CFLAGS` - a series of space separated flags passed to compilers. Note that
71 individual flags cannot currently contain spaces, so doing
72 something like: "-L=foo\ bar" is not possible.
73 * `CC` - the actual C compiler used. Note that this is used as an exact
74 executable name, so (for example) no extra flags can be passed inside
75 this variable, and the builder must ensure that there aren't any
76 trailing spaces. This compiler must understand the `-c` flag. For
77 certain `TARGET`s, it also is assumed to know about other flags (most
79 * `AR` - the `ar` (archiver) executable to use to build the static library.
81 Each of these variables can also be supplied with certain prefixes and suffixes,
82 in the following prioritized order:
84 1. `<var>_<target>` - for example, `CC_x86_64-unknown-linux-gnu`
85 2. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
86 3. `<build-kind>_<var>` - for example, `HOST_CC` or `TARGET_CFLAGS`
87 4. `<var>` - a plain `CC`, `AR` as above.
89 If none of these variables exist, cc-rs uses built-in defaults
91 In addition to the the above optional environment variables, `cc-rs` has some
92 functions with hard requirements on some variables supplied by [cargo's
93 build-script driver][cargo] that it has the `TARGET`, `OUT_DIR`, `OPT_LEVEL`,
96 [cargo]: http://doc.crates.io/build-script.html#inputs-to-the-build-script
102 Currently cc-rs supports parallel compilation (think `make -jN`) but this
103 feature is turned off by default. To enable cc-rs to compile C/C++ in parallel,
104 you can change your dependency to:
108 cc = { version = "1.0", features = ["parallel"] }
111 By default cc-rs will limit parallelism to `$NUM_JOBS`, or if not present it
112 will limit it to the number of cpus on the machine. If you are using cargo,
113 use `-jN` option of `build`, `test` and `run` commands as `$NUM_JOBS`
114 is supplied by cargo.
116 ## Compile-time Requirements
118 To work properly this crate needs access to a C compiler when the build script
119 is being run. This crate does not ship a C compiler with it. The compiler
120 required varies per platform, but there are three broad categories:
122 * Unix platforms require `cc` to be the C compiler. This can be found by
123 installing cc/clang on Linux distributions and Xcode on OSX, for example.
124 * Windows platforms targeting MSVC (e.g. your target triple ends in `-msvc`)
125 require `cl.exe` to be available and in `PATH`. This is typically found in
126 standard Visual Studio installations and the `PATH` can be set up by running
127 the appropriate developer tools shell.
128 * Windows platforms targeting MinGW (e.g. your target triple ends in `-gnu`)
129 require `cc` to be available in `PATH`. We recommend the
130 [MinGW-w64](http://mingw-w64.org) distribution, which is using the
131 [Win-builds](http://win-builds.org) installation system.
132 You may also acquire it via
133 [MSYS2](http://msys2.github.io), as explained [here][msys2-help]. Make sure
134 to install the appropriate architecture corresponding to your installation of
135 rustc. GCC from older [MinGW](http://www.mingw.org) project is compatible
136 only with 32-bit rust compiler.
138 [msys2-help]: http://github.com/rust-lang/rust#building-on-windows
142 `cc-rs` supports C++ libraries compilation by using the `cpp` method on
150 .cpp(true) // Switch to C++ library compilation.
152 .compile("libfoo.a");
156 When using C++ library compilation switch, the `CXX` and `CXXFLAGS` env
157 variables are used instead of `CC` and `CFLAGS` and the C++ standard library is
158 linked to the crate target.
162 `cc-rs` is primarily distributed under the terms of both the MIT license and
163 the Apache License (Version 2.0), with portions covered by various BSD-like
166 See LICENSE-APACHE, and LICENSE-MIT for details.