Skip to content

Commit ac20edd

Browse files
bobbobudziq
authored andcommitted
Add 'Compile a C library while setting custom defines' (#295) (#323)
Add 'Compile a C library while setting custom defines'
1 parent 39599ef commit ac20edd

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/build_tools.md

+72-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ See crates.io's [documentation on the matter][build-script-docs] for more inform
1010
|--------|--------|------------|
1111
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
1212
| [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
13-
13+
| [Compile a C library while setting custom defines][ex-cc-custom-defines] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
1414

1515
[ex-cc-static-bundled]: #ex-cc-static-bundled
1616
<a name="ex-cc-static-bundled"></a>
@@ -171,14 +171,84 @@ fn main(){
171171
}
172172
```
173173

174+
[ex-cc-custom-defines]: #ex-cc-custom-defines
175+
<a name="ex-cc-custom-defines"></a>
176+
## Compile a C library while setting custom defines
177+
178+
[![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools]
179+
180+
It is simple to build bundled C code with custom defines using [`cc::Build::define`].
181+
It takes an [`Option`] value, so it is possible to create defines such as `#define APP_NAME "foo"`
182+
as well as `#define WELCOME` (pass `None` as the value for a value-less defne). This example builds
183+
a bundled C file with dynamic defines set in `build.rs` and prints "**Welcome to foo - version 1.0.2**"
184+
when run. Cargo sets some [environment variables][cargo-env] which may be useful for some custom defines.
185+
186+
187+
### `Cargo.toml`
188+
189+
```toml
190+
[package]
191+
...
192+
version = "1.0.2"
193+
build = "build.rs"
194+
195+
[build-dependencies]
196+
cc = "1"
197+
```
198+
199+
### `build.rs`
200+
201+
```rust,no_run
202+
extern crate cc;
203+
204+
fn main() {
205+
cc::Build::new()
206+
.define("APP_NAME", "\"foo\"")
207+
.define("VERSION", format!("\"{}\"", env!("CARGO_PKG_VERSION")).as_str())
208+
.define("WELCOME", None)
209+
.file("src/foo.c")
210+
.compile("foo");
211+
}
212+
```
213+
214+
### `src/foo.c`
215+
216+
```c
217+
#include <stdio.h>
218+
219+
void print_app_info() {
220+
#ifdef WELCOME
221+
printf("Welcome to ");
222+
#endif
223+
printf("%s - version %s\n", APP_NAME, VERSION);
224+
}
225+
```
226+
227+
### `src/main.rs`
228+
229+
```rust,ignore
230+
extern {
231+
fn print_app_info();
232+
}
233+
234+
fn main(){
235+
unsafe {
236+
print_app_info();
237+
}
238+
}
239+
```
240+
174241
{{#include links.md}}
175242

176243
<!-- Other Reference -->
177244

178245
[build-script-docs]: http://doc.crates.io/build-script.html
179246
[playground]: https://play.rust-lang.org
180247
[cc-build]: https://docs.rs/cc/*/cc/struct.Build.html
248+
[`cc::Build::define`]: https://docs.rs/cc/*/cc/struct.Build.html#method.define
181249
[cc-build-include]: https://docs.rs/cc/*/cc/struct.Build.html#method.include
182250
[cc-build-flag]: https://docs.rs/cc/*/cc/struct.Build.html#method.flag
183251
[cc-build-compile]: https://docs.rs/cc/*/cc/struct.Build.html#method.compile
184-
[cc-build-cpp]: https://docs.rs/cc/*/cc/struct.Build.html#method.cpp
252+
[cc-build-cpp]: https://docs.rs/cc/*/cc/struct.Build.html#method.cpp
253+
[`Option`]: https://doc.rust-lang.org/std/option/enum.Option.html
254+
[cargo-env]: http://doc.crates.io/environment-variables.html#environment-variables-cargo-sets-for-crates

src/intro.md

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ community. It needs and welcomes help. For details see
136136
|--------|--------|------------|
137137
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
138138
| [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
139+
| [Compile a C library while setting custom defines][ex-cc-custom-defines] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
139140

140141
{{#include links.md}}
141142

@@ -147,6 +148,7 @@ community. It needs and welcomes help. For details see
147148
[ex-byteorder-le]: basics.html#ex-byteorder-le
148149
[ex-cc-static-bundled]: build_tools.html#ex-cc-static-bundled
149150
[ex-cc-static-bundled-cpp]: build_tools.html#ex-cc-static-bundled-cpp
151+
[ex-cc-custom-defines]: build_tools.html#ex-cc-custom-defines
150152
[ex-check-broken-links]: net.html#ex-check-broken-links
151153
[ex-check-cpu-cores]: basics.html#ex-check-cpu-cores
152154
[ex-clap-basic]: app.html#ex-clap-basic

0 commit comments

Comments
 (0)