Skip to content

Instantly share code, notes, and snippets.

@retep998
Created October 16, 2016 05:29
Show Gist options
  • Save retep998/1a257d815a22f2b69f48ed20c19134ab to your computer and use it in GitHub Desktop.
Save retep998/1a257d815a22f2b69f48ed20c19134ab to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
extern "C" {
void bar() {
cout << "C++ throwing an exception" << endl;
throw 273;
}
void foo();
}
int main() {
cout << "C++ main function" << endl;
try {
foo();
} catch (int e) {
cout << "C++ catching an exception" << endl;
}
}
struct Foo;
impl Drop for Foo {
fn drop(&mut self) {
println!("Rust destructor is firing");
}
}
extern "C" {
fn bar();
}
#[no_mangle]
pub extern "C" fn foo() {
let f = Foo;
println!("Rust creating object with destructor");
unsafe {
bar();
}
}
> cl /c bar.cpp /EHs /Zi; rustc -g foo.rs --crate-type=staticlib; link foo.lib bar.ob
j ws2_32.lib advapi32.lib userenv.lib shell32.lib; ./bar.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
bar.cpp
warning: unused variable: `f`, #[warn(unused_variables)] on by default
--> foo.rs:15:9
|
15 | let f = Foo;
| ^
note: link against the following native artifacts when linking against this static library
note: the order and any duplication can be significant on some platforms, and so may need to be preserved
note: library: ws2_32
note: library: userenv
note: library: shell32
note: library: advapi32
note: library: msvcrt
Microsoft (R) Incremental Linker Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.
C++ main function
Rust creating object with destructor
C++ throwing an exception
Rust destructor is firing
C++ catching an exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment