r/cpp • • Aug 06 '26

C++26: #embed

https://www.sandordargo.com/blog/2026/08/05/cpp26-embed
137 Upvotes

42 comments sorted by

View all comments

9

u/calciferBurningBacon Aug 06 '26

The compiler reads it, the build system doesn't need to know about it

I get that what the author means here is that you don't need custom build steps, but surely the build system does need to know about embedded files for build dependency tracking? I hope that's been implemented.

4

u/ABlockInTheChain Aug 06 '26

Compilers that implemented support for this have command line arguments for setting the embed search path which work exactly like the arguments for setting the include search path.

10

u/calciferBurningBacon Aug 06 '26

That's not what I'm talking about.

You need the compiler to produce some sort of dependency file like how it does for #included files so that the build system knows to recompile the embedding source file if the embedded file changes. This probably could use the exact same dependency tracking as #included files.

1

u/Status_Force_7249 29d ago

This was my first question as well, and even if compilers do track this, it seems to me that for most use cases that people are describing I don't want this dependency. I don't want to recompile just because some opaque resource changed. The linker based solutions seem much better to me in this regard and it's strange that nobody seems to acknowledge that, especially since build times are a big pain point of the language in general. The way people are presenting it now I have a feeling it's just going to turn into another overused footgun. The article even has a section for build times and all it talks about is how fast the compiler can embed 100 MBs. The only good use cases for #embed IMO would be the ones where you need to process the resource at compile time, or need the compiler to know about it to optimize it somehow, which sounds great, but how many examples are there of that?

1

u/calciferBurningBacon 28d ago

Eh, I think the linker solutions require people to understand linker magic that most people know nothing about. I'd be curious how much of a compile-time advantage the linker approach has over creating a dedicated cpp file for a given embed.

I'd be really curious what your usecase is where you don't want the dependency tracked. I would use this for GPU shaders, small embedded icons, or text templates, and I can't imagine changing any of those and not wanting the code that embeds it to get rebuilt with the new version automatically.

1

u/Status_Force_7249 25d ago

If you keep them in a separate translation unit that wouldn't be much worse than a linker, but still technically worse. I tried so called linker magic once with gcc/make and it wasn't too difficult to figure out, and if it is difficult for any given linker or build system, then that's the place to work on to improve the ergonomics IMO, rather than pushing a niche feature into the language standard and then going out of your way to sell it as the ultimate solution.

GPU shaders, small embedded icons, or text templates - that's the exact use cases I'm talking about. I do want a rebuild, but I see no reason to compile any C++ for those changes, I'd have only the linker invoked. A use case where I would want the compiler would be something like a small lookup table that you'd do some arithmetic with and have the compiler optimize it for you (and it must be small to be optimized effectively, so all that talk of how fast megabytes are embedded is moot).

Again if you keep all opaque resource embeds in a separate translation unit that would be fine (not the best, just fine), but the way everyone's talking about #embed I have a feeling that's not what's going to happen. I see a lot of constexpr and no mention of extern, so following that line of thinking people will just embed everything in headers, and then all the code that refers to the aforementioned shader, icon or text will need to be recompiled every time those change.