-
Notifications
You must be signed in to change notification settings - Fork 497
Unable to link stdc++ for example when building rust library #1206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sorry I don't understand how you are calling Just including the header file won't work, and linking with stdc++ also won't work. C++ ABI is different from rust with its own name demangles so that it can support function overloading. |
test.cpp
lib_x_example.rs
|
Thanks. And can you also show me the build.rs ? Because you would need it to be put in the workspace, not in src or examples, but in the workspace Only when it's in there, it would be automatically picked up by cargo, and run by cargo to compile and link the c++ file. |
Yes, build.rs is in the workspace, as well as Cargo.toml, Cargo.lock, and the target directory. I left them out of the initial issue for simplicity. The relevant section of build.rs is listed above. |
Thanks. I think you would have to define #[link(name = "test_cpp")]
pub extern { fn test_cpp(); } in the |
That does not work. The example still fails to compile. |
Can you remove .cpp_link_stdlib("stdc++") I think using the default |
No, that value is correct for my platform. Removing it does not help. Would you mind reproducing on your end? It seems you are just taking guesses here and I have already tried these things. Another clue: when I wrap the c++ function in my lib.rs and call that method from my example, then I am also able to use the c++ function directly in my example. |
Sorry I don't have access to my computer for now.
Yes, that's pretty much how most crates use cc and external crates. Creates bindings for it in rust library crate, then call the library crate. I think the error you experience might be due to how rust links crates. AFAIK, cargo does a linking per crate to produce rlib, so if the external library function isn't used in the library crate, it might strip out the unused linkage to stdc++. Since rlib isn't stablised and mostly internal to cargo, there isn't much documentation on it. For that particular behavior, it might be faster to ask on rust's official zulip in t-cargo channel |
Problem: cc is unable to find stdc++ when building an example for a library crate.
✅ works: binary crate, call c++ from example
✅ works: library crate, call c++ from library
❌ does not work: library crate, call c++ from example
build.rs:
Error output:
Environment: Raspberry Pi OS (aarch64) and Ubuntu 24.04 (amd64) with fresh install of Rust: cargo 1.81.0, cc="1.1.18"
I have tried setting various options in
build.rs
such as manually specifying the path to/lib/aarch64-linux-gnu/libstdc++.so.6
The text was updated successfully, but these errors were encountered: