Skip to content

Commit

Permalink
working autocxx local demo
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelraz committed Jan 2, 2025
1 parent 21b1524 commit 846be33
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
6 changes: 6 additions & 0 deletions exercise-templates/cpp-interop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
autocxx = "0.27.0"
cxx = "1.0.78"

[build-dependencies]
autocxx-build = "0.27.0"
miette = { version = "5", features = ["fancy"] } # optional but gives nicer error messages!
17 changes: 17 additions & 0 deletions exercise-templates/cpp-interop/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() -> miette::Result<()> {
let path = std::path::PathBuf::from("src");
let mut b = autocxx_build::Builder::new("src/main.rs", [&path]).build()?;
b.flag_if_supported("-std=c++14").compile("autocxx-demo");

println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/input.h");
Ok(())
}
35 changes: 35 additions & 0 deletions exercise-templates/cpp-interop/src/input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#pragma once

#include <cstdint>
#include <sstream>
#include <stdint.h>
#include <string>

class Goat {
public:
Goat() : horns(0) {}
void add_a_horn();
std::string describe() const;
private:
uint32_t horns;
};

inline uint32_t DoMath(uint32_t a) {
return a * 3;
}

inline void Goat::add_a_horn() { horns++; }
inline std::string Goat::describe() const {
std::ostringstream oss;
std::string plural = horns == 1 ? "" : "s";
oss << "This goat has " << horns << " horn" << plural << ".";
return oss.str();
}
27 changes: 25 additions & 2 deletions exercise-templates/cpp-interop/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
fn main() {
println!("Hello, world!");
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use autocxx::prelude::*;
include_cpp! {
#include "input.h"
safety!(unsafe_ffi)
generate!("DoMath")
generate!("Goat")
}

fn main() {
println!("Hello, world! - C++ math should say 12={}", ffi::DoMath(4));
let mut goat = ffi::Goat::new().within_box();
goat.as_mut().add_a_horn();
goat.as_mut().add_a_horn();
assert_eq!(
goat.describe().as_ref().unwrap().to_string_lossy(),
"This goat has 2 horns."
);
}

0 comments on commit 846be33

Please sign in to comment.