Skip to content

Proposal: ability to statically link against C++ stdlib #1497

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

Merged
merged 5 commits into from
Jul 30, 2025

Conversation

Vaider7
Copy link
Contributor

@Vaider7 Vaider7 commented Jul 16, 2025

Hi everyone!

I want to create a little proposal. It's very common situation when you need to statically link C++ stdlib to your binary. Usually it makes your binary a little bit more portable, especially if your app will be distributed to old systems. So this PR introduce an ability to explicitly say that you want to statically link C++ stdlib

Also maybe it will be useful to have a function to set a path where stdlib in .a format can be found, but I'm not sure about this one

Copy link
Collaborator

@NobodyXu NobodyXu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, would you be able to add a test case for it?

Or just test that it works for your local project?

@Vaider7
Copy link
Contributor Author

Vaider7 commented Jul 18, 2025

I would be happy to add test case for it, but It will be highly depending on host environment. I mean for environment where this test will be run, we need to be sure that somewhere in the system there's a file libstdc++.a (as example), which is available for linking, otherwise test will fail.

If it's not a problem to set CI to meet this requirement, I'll do that

Also I tested it locally for my project and it works as expected

@NobodyXu
Copy link
Collaborator

Yes you can change our ci to install the libstdc++.a

@Vaider7
Copy link
Contributor Author

Vaider7 commented Jul 29, 2025

Took a look at tests, If I got it right, the main way of testing is checking emitted C compiler flags, but I'm a little bit confused how to test static linking of C++ stdlib, because it goes to stdout as cargo:rustc-link-lib=static=stdc++

@NobodyXu Could you approximately describe how do you see the test case please? I'm stuck at this point

@NobodyXu
Copy link
Collaborator

Took a look at tests, If I got it right, the main way of testing is checking emitted C compiler flags, but I'm a little bit confused how to test static linking of C++ stdlib, because it goes to stdout as cargo:rustc-link-lib=static=stdc++

@NobodyXu Could you approximately describe how do you see the test case please? I'm stuck at this point

https://github.com/rust-lang/cc-rs/blob/main/dev-tools/cc-test/build.rs

we have a build script to test cc functionality, I think you can add another workspace there

@Vaider7
Copy link
Contributor Author

Vaider7 commented Jul 29, 2025

Wrote the test for Linux. I do not like the part with invoking g++ to find libstdc++.a file, but it's the best I can come up with. Unfortunately, linker will not automatically find static .a file like it does for shared .so file

Hope you have some ideas about that

@Vaider7 Vaider7 requested a review from NobodyXu July 30, 2025 06:43
Copy link
Collaborator

@NobodyXu NobodyXu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I think the test is good enough to ensure it's working!

@NobodyXu NobodyXu merged commit 20c06cf into rust-lang:main Jul 30, 2025
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants