static linking

master
Andrew Johnson 6 years ago
parent 0df8384ba7
commit fbb1438cda

@ -0,0 +1 @@
src/*.o

@ -1,6 +1,7 @@
[package]
name = "elevator"
version = "1.0.0"
build = "build.rs"
[dependencies]
floating-duration = "0.1.2"
@ -9,6 +10,7 @@ timebomb = "0.1"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
libc = "0.2"
[[bin]]
name = "simulate_trip"

@ -0,0 +1,3 @@
Linked object files are platform dependent. The C files and headers are provided, and a build/link script is provided.
The build script is somewhat platform dependent and requires GCC to be installed. It may be necessary to adjust build.rs
to compile this Chapter's code.

@ -0,0 +1,33 @@
use std::process::Command;
use std::env;
use std::path::Path;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
Command::new("gcc").args(&["src/motor1.c", "-c", "-fPIC", "-o"])
.arg(&format!("{}/motor1.o", out_dir))
.status().unwrap();
Command::new("ar").args(&["crus", "libmotor1.a", "motor1.o"])
.current_dir(&Path::new(&out_dir))
.status().unwrap();
Command::new("gcc").args(&["src/motor2.c", "-c", "-fPIC", "-o"])
.arg(&format!("{}/motor2.o", out_dir))
.status().unwrap();
Command::new("ar").args(&["crus", "libmotor2.a", "motor2.o"])
.current_dir(&Path::new(&out_dir))
.status().unwrap();
Command::new("gcc").args(&["src/motor3.c", "-c", "-fPIC", "-o"])
.arg(&format!("{}/motor3.o", out_dir))
.status().unwrap();
Command::new("ar").args(&["crus", "libmotor3.a", "motor3.o"])
.current_dir(&Path::new(&out_dir))
.status().unwrap();
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=static=motor1");
println!("cargo:rustc-link-lib=static=motor2");
println!("cargo:rustc-link-lib=static=motor3");
}

@ -1,6 +1,7 @@
extern crate libc;
mod motor_controllers;
mod elevator_drivers;
mod buildings;
mod physics;
mod trip_planning;
pub mod motor_controllers;
pub mod elevator_drivers;
pub mod buildings;
pub mod physics;
pub mod trip_planning;

@ -0,0 +1,6 @@
int motor1_adjust_motor(int target_force){
//real driver would do something here
//interface with physical components
return 0;
}

@ -0,0 +1,6 @@
int motor2_adjust_motor(int target_force){
//real driver would do something here
//interface with physical components
return 0;
}

@ -0,0 +1,6 @@
int motor3_adjust_motor(int target_force){
//real driver would do something here
//interface with physical components
return 0;
}

@ -0,0 +1,16 @@
use libc::c_int;
#[link(name = "motor1")]
extern {
pub fn motor1_adjust_motor(target_force: c_int) -> c_int;
}
#[link(name = "motor2")]
extern {
pub fn motor2_adjust_motor(target_force: c_int) -> c_int;
}
#[link(name = "motor3")]
extern {
pub fn motor3_adjust_motor(target_force: c_int) -> c_int;
}

@ -1,3 +1,15 @@
extern crate elevator;
use elevator::motor_controllers::{motor1_adjust_motor,motor2_adjust_motor,motor3_adjust_motor};
fn main(){
unsafe {
println!("motor1 adjust");
motor1_adjust_motor(0);
println!("motor2 adjust");
motor2_adjust_motor(0);
println!("motor3 adjust");
motor3_adjust_motor(0);
}
}

Loading…
Cancel
Save