Rust library for build scripts to compile C/C++ code into a Rust library

License: Other

Language: Rust


A library to compile C/C++/assembly into a Rust library/application.


A simple library meant to be used as a build dependency with Cargo packages in order to build a set of C/C++ files into a static archive. This crate calls out to the most relevant compiler for a platform, for example using cl on MSVC.

Using cc-rs

First, you'll want to both add a build script for your crate ( and also add this crate to your Cargo.toml via:

cc = "1.0"

Next up, you'll want to write a build script like so:


fn main() {

And that's it! Running cargo build should take care of the rest and your Rust application will now have the C files foo.c and bar.c compiled into a file named libfoo.a. You can call the functions in Rust by declaring functions in your Rust code like so:

extern {
    fn foo_function();
    fn bar_function();

pub fn call() {
    unsafe {

fn main() {
    // ...

External configuration via environment variables

To control the programs and flags used for building, the builder can set a number of different environment variables.

  • CFLAGS - a series of space separated flags passed to compilers. Note that individual flags cannot currently contain spaces, so doing something like: "-L=foo\ bar" is not possible.
  • CC - the actual C compiler used. Note that this is used as an exact executable name, so (for example) no extra flags can be passed inside this variable, and the builder must ensure that there aren't any trailing spaces. This compiler must understand the -c flag. For certain TARGETs, it also is assumed to know about other flags (most common is -fPIC).
  • AR - the ar (archiver) executable to use to build the static library.
  • 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.

Each of these variables can also be supplied with certain prefixes and suffixes, in the following prioritized order:

  1. <var>_<target> - for example, CC_x86_64-unknown-linux-gnu
  2. <var>_<target_with_underscores> - for example, CC_x86_64_unknown_linux_gnu
  3. <build-kind>_<var> - for example, HOST_CC or TARGET_CFLAGS
  4. <var> - a plain CC, AR as above.

If none of these variables exist, cc-rs uses built-in defaults

In addition to the above optional environment variables, cc-rs has some functions with hard requirements on some variables supplied by cargo's build-script driver that it has the TARGET, OUT_DIR, OPT_LEVEL, and HOST variables.

Optional features


Currently cc-rs supports parallel compilation (think make -jN) but this feature is turned off by default. To enable cc-rs to compile C/C++ in parallel, you can change your dependency to:

cc = { version = "1.0", features = ["parallel"] }

By default cc-rs will limit parallelism to $NUM_JOBS, or if not present it will limit it to the number of cpus on the machine. If you are using cargo, use -jN option of build, test and run commands as $NUM_JOBS is supplied by cargo.

Compile-time Requirements

To work properly this crate needs access to a C compiler when the build script is being run. This crate does not ship a C compiler with it. The compiler required varies per platform, but there are three broad categories:

  • Unix platforms require cc to be the C compiler. This can be found by installing cc/clang on Linux distributions and Xcode on OSX, for example.
  • Windows platforms targeting MSVC (e.g. your target triple ends in -msvc) require cl.exe to be available and in PATH. This is typically found in standard Visual Studio installations and the PATH can be set up by running the appropriate developer tools shell.
  • Windows platforms targeting MinGW (e.g. your target triple ends in -gnu) require cc to be available in PATH. We recommend the MinGW-w64 distribution, which is using the Win-builds installation system. You may also acquire it via MSYS2, as explained here. Make sure to install the appropriate architecture corresponding to your installation of rustc. GCC from older MinGW project is compatible only with 32-bit rust compiler.

C++ support

cc-rs supports C++ libraries compilation by using the cpp method on Build:

fn main() {
        .cpp(true) // Switch to C++ library compilation.

When using C++ library compilation switch, the CXX and CXXFLAGS env variables are used instead of CC and CFLAGS and the C++ standard library is linked to the crate target. Remember that C++ does name mangling so extern "C" might be required to enable rust linker to find your functions.

CUDA C++ support

cc-rs also supports compiling CUDA C++ libraries by using the cuda method on Build (currently for GNU/Clang toolchains only):

fn main() {
        // Switch to CUDA C++ library compilation using NVCC.
        // Generate code for Maxwell (GTX 970, 980, 980 Ti, Titan X).
        // Generate code for Maxwell (Jetson TX1).
        // Generate code for Pascal (GTX 1070, 1080, 1080 Ti, Titan Xp).
        // Generate code for Pascal (Tesla P100).
        // Generate code for Pascal (Jetson TX2).


This project is licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in cc-rs by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Project Statistics

Sourcerank 19
Repository Size 1.9 MB
Stars 657
Forks 230
Watchers 9
Open issues 43
Dependencies 0
Contributors 159
Tags 120
Last updated
Last pushed

Top Contributors See all

Alex Crichton Michael Rybakov Marco Satti Sean Leather Peter Jin Brian Anderson Daniel Lockyer Josh Matthews jehiggs Jonathan Kollasch Jeff Waugh Jorge Aparicio Nmlgc KokaKiwi Niels Grewe Valerii Hiora Kornel George Hahn Pierre Krieger Thomas Bracht Laumann Jespersen

Packages Referencing this Repo

A build-time dependency for Cargo build scripts to assist in invoking the native C compiler to co...
Latest release 1.0.60 - Updated - 657 stars
**Deprecated** crate, renamed to `cc` A build-time dependency for Cargo build scripts to assist ...
Latest release 0.3.55 - Updated - 657 stars

Recent Tags See all

1.0.60 September 16, 2020
1.0.59 August 18, 2020
1.0.58 July 08, 2020
1.0.57 July 02, 2020
1.0.56 June 29, 2020
1.0.55 June 25, 2020
1.0.54 May 19, 2020
1.0.53 May 14, 2020
1.0.52 April 22, 2020
1.0.51 April 22, 2020
1.0.50 January 08, 2020
1.0.49 January 06, 2020
1.0.48 December 04, 2019
1.0.47 November 05, 2019
1.0.46 October 16, 2019

Interesting Forks See all

Rust library for build scripts to compile C/C++ code into a Rust library
Rust - Other - Last pushed - 1 stars
Build library helpful in invoking gcc as a C compiler for Rust
Rust - Updated - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2020-09-16 15:47:03 UTC

Login to resync this repository