r/cpp • • Oct 05 '26

I dislike Rust libraries with a C++ API

Cargo compilation model only really works if you are creating a monolithic shared library or an executable. If you are using a library like icu4x or libffi, you can't statically link your dependencies and even if you dynamically link your dependencies, your application size will increase by orders of magnitude because your shared libraries are all statically linked, kinda.

Monolithic shared library approach also doesn't work on all libraries, for example you can't link libraries like oneTBB and OpenMP more than once.

So overall I feel rust compilation model is a "series of hacks" and isn't worth integrating to existing C++ applications, unless you use bazel*, I'm not sure about that but people in google seem to have solved this.

0 Upvotes

35 comments sorted by

7

u/pedersenk Oct 05 '26

Its refreshing to hear these valid concerns. I also agree and feel this is a big problem, slowing down the uptake of Rust.

But honestly, the vast majority of Rust developers tend not to think about things like this and work on small projects or pull in so much cruft from crates.io that its all a spaghetti mess anyway...

3

u/TheRavagerSw Oct 05 '26

Hey, what is this "Rewrite in C++" meme is about?
I have no clue why these people keep spamming it.

12

u/UndefFox Oct 05 '26

Probably because people constantly were saying that "Rust is better" and "Rewrite it in Rust" was very common thing. Hence, since it's the Rust causing the problem, they just reverse the joke to be rewrite it in C++.

Also... Rewrite it in C++

5

u/pedersenk Oct 05 '26

Heh, yeah its a meme. Its targetted at the comical Rust community rather than directed at you.

My actual technical recommendation will be to find an alternative library written in C that solves the same problem.

Not only is this directly consumable by C++ but will ensure future resilience (and 99% of Rust crates are just bindings cruft wrapping core C libraries anyway).

-1

u/13steinj Oct 05 '26

I can't tell if it's all bots or not. They've all hidden their post history.

6

u/Electronic_Tap_8052 Oct 05 '26

Rust isn't used much in real code, mostly toy projects and stuff intended to be ingested as crates.

7

u/pjmlp Oct 06 '26

Yeah, toy projects like Linux kernel, Windows, AWS and Azure infrastructure.

1

u/Electronic_Tap_8052 Oct 06 '26

That's quite the list.

36

u/Sfacm Oct 05 '26

Rewrite it in C++

30

u/PersonalDatabase31 Oct 05 '26

Rewrite it in C++

21

u/AdOnly69 Oct 05 '26

Rewrite it in C++

18

u/tatstytastycakes C++ Fool Oct 05 '26 edited Oct 05 '26

Rewrite it in C++. Literally nobody is telling you that you can't.

35

u/[deleted] Oct 05 '26

Rewrite it in C++

34

u/sooka_bazooka Oct 05 '26

Rewrite it in C++

14

u/blogoman Oct 05 '26

You could post about it.

Or you could rewrite it in C++.

38

u/KFUP Oct 05 '26

Rewrite it in C++

34

u/OccasionThin7697 Oct 05 '26

Rewrite it in C++

34

u/XTBZ Oct 05 '26

Rewrite it in C++

33

u/Zealousideal_Sort521 Oct 05 '26

Rewrite it in C++

22

u/James20k P2005R0 Oct 05 '26

On one hand: I think this thread probably doesn't meet the bar for something we should keep on /r/cpp, but on the other hand I'm a grade A shitpost enthusiast so I'm torn on deleting this. Anyway:

Rewrite it in C++

7

u/Wild_Meeting1428 Oct 05 '26

Layer 8 problem?

7

u/Nothing_from_void Oct 05 '26

yeah isn't that the idea behind bazel, mixing build systems from multiple languages?

1

u/ts826848 Oct 05 '26

Do you mind elaborating on what exactly you mean by "cargo/rust compilation model"? I'm not sure I quite understand what you mean by that.

0

u/TheRavagerSw Oct 05 '26

Compiling rust projects via cargo maybe with extensions like cargo-c

1

u/ts826848 Oct 05 '26

Hmmm, ok. Can you explain a bit more about "your shared libraries are all statically linked, kinda" and "series of hacks"? I still don't feel I fully understand your intended use case(s).

3

u/TheRavagerSw Oct 05 '26

Assume you have statically linked application, and you want to swap out library A and B with rust equivalent C and D.

If you compile C and D with cargo and produce an archive, and link it to your C++ application, following things may happen:

a) You might link different versions of the same crate
b) Your crates might link a dependency that must only be linked once

To solve this you can dynamically link the libraries, but assume that each rust library uses 4 crates as dependencies, if your both your libraries dependencies are identical you are effectively doubling your binary size.

1

u/ts826848 Oct 05 '26

Hrm, that's an interesting use case indeed. Thanks for taking the time to explain!

Unfortunately I'm no Cargo guru so I can't offer good advice :/ Worst case I guess you'd have to drive rustc manually which is what I think Bazel/etc. are doing anyways, though given Rust's relatively heavy use of generics I'd be curious how much space savings you shoudl expect in the first place.

1

u/TheRavagerSw Oct 05 '26

Not small numbers, go look up slint, it's runtime binary is very large.

1

u/ts826848 Oct 05 '26

Sure, I know you can save space using shared libraries; I'm just wondering how much space you'll actually save since as far as I know the amount of savings you see depends pretty heavily on what the API looks like. In particular, the way Rust (currently?) handles generics (i.e., monomorphization) isn't ideal for shared libraries, similar to C++ templates (plus the fact that Rust doesn't have an extern template equivalent).

I'm not familiar with Slint so I can't speak as to whether my musings apply to it; perhaps its the case that it doesn't (ab)use generics as heavily and so can hypothetically use shared libraries to share space. At that point there's probably ABI stability questions to deal with, though.

1

u/TheRavagerSw Oct 06 '26

A lot of space actually. As your project scales up your size will grow up exponentially if you do something like this.

1

u/t_hunger Oct 06 '26

a) is actually fine in rust. It of course bloats binaries, but the symbols will nkt conflict. b) linkers should be able to sort this out: Its the same problem with C static libraries and for a long time in C's history there were no dynamic libraries at all.

5

u/Sweet-Area-1539 Oct 06 '26

Rewrite it in Brainfuck

1

u/13steinj Oct 05 '26

I feel rust compilation model is a "series of hacks" and isn't worth integrating to existing C++ applications, unless you use bazel*, I'm not sure about that but people in google seem to have solved this.

No even Bazel does not solve this problem. On could maybe argue Blaze does, but I doubt that / if they have an internal ruleset that avoids this problem.

1

u/UndefinedDefined Oct 13 '26

Just don't use C++ when using Rust - why?

For deployment statically linked dependencies make sense - user is saved, doesn't have to navigate DLL hell, and the application starts in a millisecond without spending time due to relocations.

I think dynamic libs only make sense if your app is plugin-based or if you need to take advantage of existing ecosystem - like creating a Qt/KDE/Gtk/GNOME app on Linux. Rust can use dynamically linked libs btw., it's just not how rust dependencies are linked by design, unless they provide C-API, which is used as a boundary basically.

Try to compile servo via cargo, and try to compile chromium - you will see.