Skip to content

Commit 9b9378f

Browse files
committed
Compile a C library while setting custom defines
1 parent be72b2d commit 9b9378f

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

build_time_tooling/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,14 @@ fn main() {
55
.cpp(true)
66
.file("src/foo.cpp")
77
.compile("foo");
8+
9+
cc::Build::new()
10+
.define("APP_NAME", "\"bar\"")
11+
.define(
12+
"VERSION",
13+
format!("\"{}\"", env!("CARGO_PKG_VERSION")).as_str(),
14+
)
15+
.define("WELCOME", None)
16+
.file("src/bar.c")
17+
.compile("bar");
818
}

build_time_tooling/src/bar.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
3+
void print_app_info() {
4+
#ifdef WELCOME
5+
printf("Welcome to ");
6+
#endif
7+
printf("%s - version %s\n", APP_NAME, VERSION);
8+
}

build_time_tooling/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern "C" {
2121
fn hello();
2222
fn greet(name: *const c_char);
2323
fn multiply(x: i32, y: i32) -> i32;
24+
fn print_app_info();
2425
}
2526

2627
fn main() -> Result<()> {
@@ -31,5 +32,8 @@ fn main() -> Result<()> {
3132
unsafe {
3233
println!("{}", multiply(5, 7));
3334
}
35+
unsafe {
36+
print_app_info();
37+
}
3438
Ok(())
3539
}

0 commit comments

Comments
 (0)