Linking failed with rust bindgen but not the cc crate [bountied SO question]
Hi there. I'm trying to call raw glfw and opengl functions in a rust project. I'm able to get this working when I write my own functions in C, call them in Rust, and compile with the cc crate. However when I try and do a similar thing with rust bindgen, I get lots of `undefined symbols` errors, despite telling rustc to link the libraries in the same way.
This is my build.rs script
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rustc-link-lib=framework=OpenGL");
println!("cargo:rustc-link-lib=glfw");
let bindings = bindgen::Builder::default()
.header("src/wrapper.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
Calling cargo build gives me tons of undefined symbols errors related to glfw. I actually wrote this question up in much more detail on Stack Overflow. It got very little attention so I put a bounty on it of 50 reputation. This has become a real roadblock in a project I want to work on, and if someone could help me out or point me in the right direction, that would be amazing.
Posted Nov 14, 2020, 7:16 PM UTC
Suggestion for the next time you ask questions like these: don't omit the details. In particular the
// giant wall of options passed to rustcis important in this case.As things are now we can only guess. In particular I suspect the problem may be a traditional
-largument mis-ordering. The ordering of these arguments matters and it may very well be the case that the ordering you have currently is incorrect. One of similar reports of such an issue is this.This is a great point and I thought about including it, but worried that I already had too many details. I will update my question as soon as I am by my computer. Do you think I should include the entire wall of arguments passed to the compiler when I run
cargo build —verbose(which is when you can see that rustc is receiving -l glfw -l framework=OpenGL) or snip some of that where it makes sense ?I’ve updated it with the full error message from
cargo run —verbose. Feel free to suggest any more critiques or just edit the question directly if you feel like it. Thanks so much 😊Edit: PS I tried reoreding the lines linking the libraries in the build script and it didn’t change anything
So, looking closely at these lines, the
[lib]target does get the-l glfw, but the[bin]target does not. This is also reflected in the linker (cc) command line, which is also missing the-l glfwwhen linking the binary.As a first troubleshooting step I would suggest trying to remove the
[lib]target (by removing or renaming thesrc/lib.rs) from the crate, and use just use the[bin]target.This is the correct answer. When using
rustc-link-lib, the symbols will only be available to the library. This is explained a bit at https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-link-lib.I also see a
pub mod lib;, which is generally not the correct way to use a library. The library should be accessed as a crate, or you shouldn't define a library at all.Thank you so much. That explanation and link really helped my understanding. Following u/simukis 's advice by renaming lib.rs to foo.rs fixed my problem (As did just moving the `include!(concat!(env!("OUT_DIR"), "/bindings.rs"));` line to main.rs).
And looks like my ideas about the rust file hierarchy were either outdated or wrong. Thanks!
This fixed it! Can't thank you enough. Both moving `include!(concat!(env!("OUT_DIR"), "/bindings.rs"));` to `main.rs` while removing `lib.rs` , and renaming `lib.rs` to foo.rs fixed the linking and I can successfully open a window with OpenGL!
Feel free to submit this as an answer to the stack overflow question and I will happily accept it and give you the bounty.
Thanks again!