3 A library to compile C/C++ code into a Rust library/application.
5 [![Build Status](https://travis-ci.org/alexcrichton/gcc-rs.svg?branch=master)](https://travis-ci.org/alexcrichton/gcc-rs)
6 [![Build status](https://ci.appveyor.com/api/projects/status/onu270iw98h81nwv?svg=true)](https://ci.appveyor.com/project/alexcrichton/gcc-rs)
8 [Documentation](https://docs.rs/gcc)
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. Note that while this
12 crate is called "gcc", it actually calls out to the most relevant compile for
13 a platform, for example using `cl` on MSVC. That is, this crate does indeed work
18 First, you'll want to both add a build script for your crate (`build.rs`) and
19 also add this crate to your `Cargo.toml` via:
30 Next up, you'll want to write a build script like so:
38 gcc::compile_library("libfoo.a", &["foo.c", "bar.c"]);
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 it. You
44 can call the functions in Rust by declaring functions in your Rust code like so:
64 ## External configuration via environment variables
66 To control the programs and flags used for building, the builder can set a
67 number of different environment variables.
69 * `CFLAGS` - a series of space separated flags passed to "gcc". Note that
70 individual flags cannot currently contain spaces, so doing
71 something like: "-L=foo\ bar" is not possible.
72 * `CC` - the actual C compiler used. Note that this is used as an exact
73 executable name, so (for example) no extra flags can be passed inside
74 this variable, and the builder must ensure that there aren't any
75 trailing spaces. This compiler must understand the `-c` flag. For
76 certain `TARGET`s, it also is assumed to know about other flags (most
78 * `AR` - the `ar` (archiver) executable to use to build the static library.
80 Each of these variables can also be supplied with certain prefixes and suffixes,
81 in the following prioritized order:
83 1. `<var>_<target>` - for example, `CC_x86_64-unknown-linux-gnu`
84 2. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
85 3. `<build-kind>_<var>` - for example, `HOST_CC` or `TARGET_CFLAGS`
86 4. `<var>` - a plain `CC`, `AR` as above.
88 If none of these variables exist, gcc-rs uses built-in defaults
90 In addition to the the above optional environment variables, `gcc-rs` has some
91 functions with hard requirements on some variables supplied by [cargo's
92 build-script driver][cargo] that it has the `TARGET`, `OUT_DIR`, `OPT_LEVEL`,
95 [cargo]: http://doc.crates.io/build-script.html#inputs-to-the-build-script
99 Currently gcc-rs supports parallel compilation (think `make -jN`) but this
100 feature is turned off by default. To enable gcc-rs to compile C/C++ in parallel,
101 you can change your dependency to:
105 gcc = { version = "0.3", features = ["parallel"] }
108 By default gcc-rs will limit parallelism to `$NUM_JOBS`, or if not present it
109 will limit it to the number of cpus on the machine. If you are using cargo,
110 use `-jN` option of `build`, `test` and `run` commands as `$NUM_JOBS`
111 is supplied by cargo.
113 ## Compile-time Requirements
115 To work properly this crate needs access to a C compiler when the build script
116 is being run. This crate does not ship a C compiler with it. The compiler
117 required varies per platform, but there are three broad categories:
119 * Unix platforms require `cc` to be the C compiler. This can be found by
120 installing gcc/clang on Linux distributions and Xcode on OSX, for example.
121 * Windows platforms targeting MSVC (e.g. your target triple ends in `-msvc`)
122 require `cl.exe` to be available and in `PATH`. This is typically found in
123 standard Visual Studio installations and the `PATH` can be set up by running
124 the appropriate developer tools shell.
125 * Windows platforms targeting MinGW (e.g. your target triple ends in `-gnu`)
126 require `gcc` to be available in `PATH`. We recommend the
127 [MinGW-w64](http://mingw-w64.org) distribution, which is using the
128 [Win-builds](http://win-builds.org) installation system.
129 You may also acquire it via
130 [MSYS2](http://msys2.github.io), as explained [here][msys2-help]. Make sure
131 to install the appropriate architecture corresponding to your installation of
132 rustc. GCC from older [MinGW](http://www.mingw.org) project is compatible
133 only with 32-bit rust compiler.
135 [msys2-help]: http://github.com/rust-lang/rust#building-on-windows
139 `gcc-rs` supports C++ libraries compilation by using the `cpp` method on
147 .cpp(true) // Switch to C++ library compilation.
149 .compile("libfoo.a");
153 When using C++ library compilation switch, the `CXX` and `CXXFLAGS` env
154 variables are used instead of `CC` and `CFLAGS` and the C++ standard library is
155 linked to the crate target.
159 `gcc-rs` is primarily distributed under the terms of both the MIT license and
160 the Apache License (Version 2.0), with portions covered by various BSD-like
163 See LICENSE-APACHE, and LICENSE-MIT for details.