rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.81k stars 262 forks source link

Provide working example for FFI #122

Open HarrisonMc555 opened 5 years ago

HarrisonMc555 commented 5 years ago

It took me a long time to get the "snappy" example, included in the FFI section, working on my machine. As it turned out, I needed several options in a build.rs file--one of which I didn't find until I stumbled across the rust-snappy repository.

Namely, I needed the following build.rs file:

fn main() {
    println!("cargo:rustc-link-search=native=/path/to/snappy/");
    println!("cargo:rustc-link-lib=static=snappy");
    println!("cargo:include=/path/to/snappy/");

    // Need to link C with C++ files
    println!("cargo:rustc-link-lib=stdc++");
}

This is never mentioned in the FFI section, and it was a bit frustrating. I've seen it mentioned several places that it's "easy to call C from Rust", and I think giving people everything they need to know to be successful in the Rustonomicon would help make that even more true.

In addition, specifically mentioning the necessity of the stcd++ library for linking to C wrappers around C++ libraries would be great. I'm sure a lot of people are already familiar with that concept, but many others (myself included) don't know about it and would need a lot of trial and error to get that to work.

Do others agree? I'm willing to attempt a write-up if I could get some feedback on where it would be appropriate to include. My thought is to put it near the beginning so that people can get a working example from the beginning, but I'm probably biased since I just went through the experience of being confused.

Eson-Jia commented 3 years ago

we need to read this before Build Scripts

mfornet commented 1 year ago

+1, this is mandatory to run the example locally.