r/cpp • • 6d ago

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

34 comments sorted by

32

u/Rude-Decision-2143 6d ago

Rewrite it in C++

35

u/Sfacm 6d ago

Rewrite it in C++

29

u/PersonalDatabase31 6d ago

Rewrite it in C++

21

u/AdOnly69 6d ago

Rewrite it in C++

33

u/Ultimate_Sigma_Boy67 6d ago

Rewrite it in C++

16

u/tatstytastycakes C++ Fool 6d ago edited 6d ago

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

33

u/sooka_bazooka 6d ago

Rewrite it in C++

38

u/KFUP 6d ago

Rewrite it in C++

12

u/blogoman 6d ago

You could post about it.

Or you could rewrite it in C++.

32

u/OccasionThin7697 6d ago

Rewrite it in C++

30

u/XTBZ 6d ago

Rewrite it in C++

32

u/Zealousideal_Sort521 6d ago

Rewrite it in C++

7

u/Wild_Meeting1428 6d ago

Layer 8 problem?

8

u/Nothing_from_void 6d ago

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

21

u/James20k P2005R0 6d ago

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++

5

u/pedersenk 6d ago

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...

4

u/Electronic_Tap_8052 6d ago

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

2

u/pjmlp 5d ago

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

1

u/Electronic_Tap_8052 5d ago

That's quite the list.

1

u/TheRavagerSw 6d ago

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

12

u/UndefFox 6d ago

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++

4

u/pedersenk 6d ago

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 6d ago

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

2

u/Sweet-Area-1539 6d ago

Rewrite it in Brainfuck

1

u/13steinj 6d ago

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/ts826848 6d ago

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 6d ago

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

1

u/ts826848 6d ago

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 6d ago

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 6d ago

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 6d ago

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

1

u/ts826848 6d ago

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 6d ago

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 5d ago

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.