r/cpp • • Oct 15 '26

Please consider bundling C++ STL with your application

Systems targeted by C++ toolchains vary a lot even though C++ constantly changes. If you rely on operating system STL for your application code, following things may happen:

  • If your platform A supports C++20 and your platform B supports C++14 and your platform C supports C++11, you will either force C++ <= 11 to all your libraries, or use macro magic etc, to use one set of features each platform.

  • If you operate in a specialized domain, such as real-time systems, you may need a standard library built with specific configuration options, such as disabling exceptions or RTTI. Relying on the system-provided STL leaves you dependent on how it was built, which may not meet your requirements

If you build your STL of choice you solve all these issues:

  • You can use the highest C++ standard possible, you no longer have to think about each operating system state extensively.
  • You can configure your STL to match your needs

In nearly all modern system languages you build standard library yourself, I don't see any reason why C++ developers shouldn't consider this.

0 Upvotes

56 comments sorted by

13

u/DnBenjamin Oct 15 '26

I have no idea what any of this means. Do you want me to write my own implementation of the C++ standard library and maintain it on every platform I want to target instead of using MSVC STL, libstdc++, libc++, etc.?

-7

u/TheRavagerSw Oct 15 '26

I want you to build stdlib from source and link it to your application rather than linking to operating system one.

You don't have to maintain it, you can just use llvm libcxx, please do contribute there if you can though

5

u/DnBenjamin Oct 15 '26

From what source? Who’s source?

-1

u/TheRavagerSw Oct 15 '26

llvm-project

7

u/DnBenjamin Oct 15 '26

You know that llvm’s libc++ itself needs to link to msvcprt.lib on Windows right? So what did I just solve? Now I have two libraries to maintain in parallel with the work already being done by the kind people at llvm and Microsoft.

Why not just link statically to the natural std lib provided with your compiler for your target platform? msvc, GCC, and clang all support that via a couple switches.

2

u/TheRavagerSw Oct 15 '26

Libc++ no longer depends on that

4

u/DnBenjamin Oct 15 '26

Ok cool, then the llvm docs are out of date. But why do I need to build it from source instead of statically linking to the one my compiler already provides for the target platform?

-1

u/TheRavagerSw Oct 15 '26 edited Oct 15 '26

Because each stdlib is different? Suppose you are targeting Linux, you can indeed statically link distro libcxx, but that would be GCC 11.2 for Debian 12, GCC 15 for fedora GCC 16 for whatever rolling release distro.

Do you know what feature set is supported on each? If 11.2 only supports C++14, are you willing to limit yourself?

4

u/jwezorek Oct 16 '26

I don't understand why you're presenting this as general advice for C++ application developers.

Much of the standard library is implemented in headers/templates and compiled into the application already. For the parts that aren't, developers can statically link the implementation supplied with their compiler.

I can see why building your own libc++ might be useful if it is solving some problem in a cross-platform development scenario but a lot of C++ development is either not cross-platform or is trivially cross-platform. Consider someone writing a Windows application that is already using DirectX etc. so is already only targeting Windows. What problem would building libc++ rather than just using Microsoft's standard library solve?

It seems like you are describing a solution to some specific deployment or toolchain problem but treating it like the specific requirements of that case apply generally.

3

u/fdwr fdwr@github 🔍 Oct 17 '26

 What problem would building libc++ rather than just using Microsoft's standard library solve?

Yeah, I don't know about building your own CRT, but statically linking has benefits. Granted, the exe is bigger than using the UCRT (👎), and some security update possibilities are lost (👎), but also the user doesn't need to download&install some MSVC DLLs (👍), your exe is trivially copyable as a portable app rather than an app folder with a dozen "api-ms-win-*.dll" files strewn about as dependencies (👍), and runs on a broader range of Windows versions (👍). So, I default  to static linking for my apps.

Building the CRT though - little need for that pain. The only reason I could think is if you want a truly minimal .exe, as the linker seems incapable of pruning orphaned code sometimes (.e.g. I would expect a simple Hello World exe to be closer to 50KBs like they used to be, rather than 150-300KBs like more recent VS builds).

-1

u/TheRavagerSw Oct 16 '26

Most C++ libraries and applications are cross platform, if you are only doing native development and don't care about new features feel free to ignore my advice.

1

u/not_a_novel_account cmake dev Oct 17 '26

Most C++ libraries and applications are cross platform

[citation needed]

5

u/neppo95 Oct 15 '26

There's reasons why you don't even want to do this whilst being irrelevant for a lot of projects altogether. As for "nearly all modern system languages you build standard library yourself" -> That is just false. Don't spread misinformation.

2

u/TheRavagerSw Oct 15 '26

What are you talking about?

What system level languages other than C/C++ that are cross platform have stdlibs provided by each platform?

2

u/neppo95 Oct 15 '26

Most popular ones? Apart from C/C++, Rust & Swift for example. Or going a bit further back into history: Ada and D.

Zig does do it per build I believe. I'd say it is the opposite of what you said, most do not require you to compile it yourself, but yes there are a few that do. That said; my initial comment also still applies, there is no reason to for a lot of projects.

1

u/jcelerier ossia score Oct 15 '26

... does windows and macOS ship a rust stdlib .dll with the OS install ?

1

u/neppo95 Oct 15 '26

No, they do not. Nor is that what I replied to.

"In nearly all modern system languages you build standard library yourself"

1

u/13steinj Oct 16 '26

You effectively do and end up statically linking the symbols in?

2

u/neppo95 Oct 16 '26

Let’s just keep on beating around the bush shall we ;)

1

u/13steinj Oct 16 '26

The only one doing that seems to be you?

2

u/neppo95 Oct 16 '26

I respond directly to what he was saying, you respond to me talking about something I'm not talking about or isn't the point. Sure buddy, if that's all you have to say, please don't even answer to this.

0

u/TheRavagerSw Oct 15 '26

Sorry, I misread your comment, I'll reread and reply as soon as possible

2

u/neppo95 Oct 16 '26

Guessing you forgot about it or realized your post was nonsense ;)

-1

u/TheRavagerSw Oct 16 '26

I slept and went to gym :)

0

u/[deleted] Oct 16 '26

[deleted] — view deleted comment

2

u/neppo95 Oct 16 '26

What? We’re just talking here mate, I suggest you follow your own advice.

3

u/holyblackcat Oct 17 '26
  1. It's not specific to C++ stdlib, you can say this about every library.
  2. Why conflate bundling it and building from source? You can paste the prebuilt binary from your system.
  3. If done naively, it can break things. Happened to me. When using OpenGL on AMD cards, some .so from the driver gets loaded into your app, and it wants libstdc++. The one I bundled was older, so it broke. You need some clever script that analyzes what version is available at runtime, and choses between it and yours using LD_LIBRARY_PATH.

1

u/TheRavagerSw Oct 17 '26

1- Yes ideally you should depend on operating system libs as little as possible 2- Yes, but its not guaranteed to work, you can't copy libcxx from arch to run it on Debian 3- I never had such issues, probably a abi namespace will fix that

1

u/holyblackcat Oct 17 '26 edited Oct 17 '26

2 is not guaranteed to work even if you build from source. It's no different than using the one provided with your system, since guess what, they build it from source too.

can't copy libcxx from arch to run it on Debian If you build it from source on arch, it wouldn't work on debian either.

From what I know, the only thing that matters is the glibc version it was built against. So if you take a sufficiently old distro (like an old LTS Ubuntu), anything built on it should work on anything newer, assuming it also uses glibc and not musl/whatever. (And for this the prebuilt libstdc++ works fine.)

3 - What are you proposing exactly? Patching stdlib headers to add the ABI namespace?

1

u/TheRavagerSw Oct 17 '26

You can build C++ standard library against a sysroot of your choice, this gives you both worlds, broad compatibility with latest C++ features, I myself build llvm libc++ against debian 12 sysroot even though I'm on mint.

3- It is a build configuration, not necessarly a manual patch, for example if you want to build your own libc++ when targeting macos, you pass this cmake variable when building libc++ DLIBCXX_ABI_NAMESPACE if you don't you get runtime errors.

If you want more detailed info we can chat on discord, we are already friends there

1

u/holyblackcat Oct 17 '26

I see, makes sense.

20

u/ybungalobill Oct 15 '26

You are conflating STL with C++ runtime, they are not the same.

Every application under the sun bundles STL with its binaries because STL is a Standard *Template* Library and templates are instantiated and linked with your executable...

0

u/no-sig-available Oct 16 '26

You are conflating STL with C++ runtime, they are not the same.

There are indications that they are the same:

https://github.com/microsoft/STL

7

u/ybungalobill Oct 16 '26 edited Oct 16 '26

Microsoft is not an authoritative source; they are known to misuse terms. E.g. they still use 'Unicode' and 'UTF-16' as synonyms.

STL -- Standard Template Library -- originally a library from the pre-standard days that implemented generic containers and algorithms. It became part of the standard when C++ was standardized in 1998.

C++ Standard Library -- all the library features that are described by the C++ standard. It includes STL, but also C standard functions (eg std::fopen), exceptions (std::exception), locales, strings and iostreams (even though they use templates, they were never part of the STL), and nowadays threads, chrono, and much more.

C++ runtime -- the infrastructure that's needed at runtime to run your C++ code -- this includes heap implementation, interfacing with the OS file functions, implementation of exception handling and RTTI, startup code, etc...

A good example to illustrate the distinction: dynamic_cast is implemented by the C++ runtime, but it is not a part of the standard library (or STL for that matter).

The OP probably means "compile your C++ runtime with your own flags and then bundle it with your app" which is a legitimate take as long as you don't need ABI compatibility with other components.

1

u/no-sig-available Oct 16 '26

STL -- Standard Template Library -- originally a library from the pre-standard days

"Original" doesn't say that it keeps its meaning forever. If enough people use STL for the C++ standard library, the meaning of the word can change over time.

1

u/nzmjx Oct 15 '26

You cannot bundle STL with your application because it is part of standard. And you cannot build standard library with your application, because it is neither portable nor supported.

It is done this way: you target specific standard version, and if there is something available from more recent version in the library (which is not a language feature) you conditionally use it while providing fallback implementation.

7

u/13steinj Oct 15 '26 edited Oct 15 '26

It is perfectly reasonable to bundle the stdlib with your application or even just use static linking for the stdlib. The past two orgs I was at used the first approach, one of them also used the second. On linux you just set your rpath / ld path appropriately.

Windows does the moral equivalent of this, every app more or less also installs the MSVC "Redistributables."

I don't know what Apple platforms do but close enough.

E: This is pretty untenable with the C library though / glibc

2

u/TheRavagerSw Oct 15 '26

For most applications yes, but for platforms involving mini-pc's like raspberry pi, musl saves a lot of head space.

Also consider bare metal/freestanding targets, you always build libc for those

3

u/13steinj Oct 15 '26

I have had enough horror stories with musl that I still would not use it in production, personally, until compat on linux is better. On some embedded platforms uClibc is fine though, but it's insanity on traditional platforms IMO.

0

u/TheRavagerSw Oct 15 '26

That is correct, it has a lot weird stuff like 128kb stack sizes. But if you are targeting raspberry pi's you have no choice.

On there glibc is so old, my Linux sysroots don't work, even though I target Debian 12 which has a kernel version from 10-8 years ago

-1

u/nzmjx Oct 15 '26

If you don't know about Apple platforms, then it is not portable, which I mentioned. While developing in C++, usually we have more important problems than building the damn standard library.

2

u/13steinj Oct 15 '26

I don't but it's easy to search and prove you wrong?

Apple normally supplies libc++ with the OS, so some newer library features require a newer minimum OS version. Shipping your own libc++ is possible: Chromium’s build does this on Apple platforms, including static linking on iOS.

Apple's documentation to change runpath

On iOS static linking appears to be the practical approach? System frameworks can end up loading the iOS's runtime anyway, and you need to take care to avoid passing things across the implementations. But you can use a custom namespace to make the symbols have different names and be good anyway, like Chromium does.

1

u/TheRavagerSw Oct 15 '26

Custom namespaces are mandatory for some other platforms like mingw and android, if I recall correctly.

1

u/TheRavagerSw Oct 15 '26

You can actually, libstdcxx builds with GCC, libcxx is a part of llvm, you build them install them to some location and pass some flags.

Usually nostdlib++ and nostdinc++ but for Msvc it is different, then just linking like you would any other library.

-1

u/nzmjx Oct 15 '26

Try building standard library across Windows, Linux, FreeBSD, macOS, Android and iOS. After you fail, let's talk again.

1

u/TheRavagerSw Oct 15 '26

I do build standard library for all these platforms. I use libcxx but did build stdcxx for bsd and linux

1

u/Minimonium Oct 16 '26

Of course you can. You can bundle, and you can build in a way that is compatible with target platforms. You just need to build the whole app stack that uses the same standard library that way.

1

u/jcelerier ossia score Oct 15 '26

I build https://ossia.io and bundle libc++ wherever possible in https://github.com/ossia/sdk and it really solves so many problems comparing to building against the OS stdlib

1

u/stick_figure Oct 15 '26

I think this is correct. Both go and rust link statically by default (to different degrees), and you never ask yourself "will this go binary run on that target Linux system?" because you just know it will.

Modern developers should really see dynamic linking and plugin loading into the same process address space as advanced use cases, and process address space isolation and IPC as the default level of separation for code that is built separately by different people and parties.

Back in the 90s, DLLs probably made a lot of sense when you're shooting to hit certain performance and memory usage targets, but with AI and everything else going on, you want to optimize to minimize coordination overhead so you can have fritctionless fast upgrades. That means more isolation, more hermeticity between separately compiled components.

1

u/TheRavagerSw Oct 15 '26

The problem is not dynamic linking, it is relying on the operating system when you shouldn't have.

For fast iteration you want dynamic libraries anyway. Static linking is too slow even with modern linkers like mold.

You can just build stl as a dynamic library too, in addition to static.

6

u/13steinj Oct 16 '26

Yeah. But the way nearly other language solves it is to statically link everything and call it a day. Dynamically or statically linking the stdlib has tradeoffs. In most cases it's easiest just to statically link and not depend on runtime loading the dynamic library on the system. It also has other benefits around performance and total application size.

0

u/UndefinedDefined Oct 16 '26

Another choice is to not use it - then there is no problem linking to it or just linking it statically.

1

u/TheRavagerSw Oct 16 '26

well... you can, but then why do you use C++ in the first place, you won't be able to use most libraries.

0

u/UndefinedDefined Oct 17 '26

What do you mean? There are third party libs that offer much better functionality than std when it comes to almost everything. So what's the point of std library, if almost nothing it provides is actually usable in real production code? It's too fat, just because committee doesn't understand what package management is and the solution is to bundle, bundle, bundle - then you end up with 3 implementations and if both 3 are not magically good, the thing is not gonna be used.

C++ needs a lean standard library, and not the mostrum it has became.

2

u/TheRavagerSw Oct 17 '26

Nearly all these third party libs depend on standard library.

0

u/UndefinedDefined Oct 17 '26

Of course, because even if you don't include anything you would probably end up depending on it, just to have the same base type for an exception. But the point is that if you don't use the below average stuff and use third party libs instead, you are not affected by mediocre containers or catasthrofic misimplementations like std::regex.

To give you a little example, look what Chromium actually uses from std library.

0

u/13steinj Oct 15 '26

First time I see a post from you in a while and I just say "based take no notes."

For reference, Plex is a great example of a C++ application that does this. Weirdly it's the only one I can think of that does this on Linux platforms, and I don't know why. I guess in fairness most applications just rely on flatpak or separate deb/rpm maintainers instead, and I don't know why.