C/C++ Embedded Files (2013)

(4rknova.com)

27 points | by ibobev 1 hour ago

7 comments

  • gavinray 1 hour ago
    Outdated, modern solution is baked in now

    https://en.cppreference.com/w/c/preprocessor/embed

    • jcalvinowens 22 minutes ago
      It will be at least a decade before I can rely on that in systems software that needs to be portable.
    • monegator 23 minutes ago
      let me know when my embedded target's compiler is C23 compliant (i mean, i whish. we may be getting C11 or even C17 some times next year but i'm not holding my breath)
    • rolandhvar 1 hour ago
      The thing that always irks me about c++ is this sort of thing:

      > Explanation 1) Searches for the resource identified by h-char-sequence in implementation-defined manner.

      Okay, so now I have to make assumptions that the implementation is reasonable, and won't go and "search" by asking an LLM or accidentally revealing my credit card details to a third party, right?

      And even if the implementation _is_ reasonable the only way I know what "search" means in this context is by looking at an example, and the example says "it's basically a filename".

      So now I think to myself: if I want to remain portable, I'll just write a python script to do a damn substitution to embed my file, which is guaranteed to work under _any_ implementation and I don't have to worry about it as soon as I have my source file.

      Does anyone else feel this way or is it just me?

      • david2ndaccount 31 minutes ago
        If you want to remain portable, write your code in the intersection of the big 3 - GCC, Clang and MSVC - and you’ll be good enough. Other implementations will either be weird enough that many things you’d expect to work won’t or are forced to copy what those 3 do anyway.
      • CamouflagedKiwi 56 minutes ago
        This doesn't sound like the kind of portability anyone is really worried about. I get that the docs on the linked site are written in standards-ese and are complicated by macro replacement, but I don't think the outcome of sending your credit card details away is gonna be an outcome. If it was, an uncharitable implementation with access to your card details would be free to do that any time you gave it input invoking undefined behaviour (which is of course not uncommon, especially in incorrect code).
      • MoltenMan 1 hour ago
        ...what? What are you talking about? In what world would a compiler implement a preprocessor directive to ever use an llm, the internet, or your credit card details (from where would it get those)??? There are always implementation defined things in every language, for example, ub behavior. Do you get worried that someone will steal your bitcoin every time you use after free? Of course not! Even in Python when you OOM -- at least in CPython -- you crash with undefined behavior.
        • MoltenMan 1 hour ago
          Sorry for being so aggressive. I suppose I'm just very confused at where you're coming from.
  • CamouflagedKiwi 1 hour ago
    You can also do it using ld - it's something like ld -r --format binary -o out.o <file>, although you do want some build system assistance to generate header files allowing you to access the thing (somewhat similar to the assembly example here). It's a bit of a performance but I strongly prefer it to generating header files in the earlier options - those header files can end up being _very_ large (they generally multiply up the size of the embedded file by 2-4x) and slow to compile.

    All a bit less relevant now since recent C++ versions have this built in by default. Generally something languages have been IMO too slow on (e.g. Go picked this up four or so years ago, after a bunch of less nice home-grown alternatives), it's actually just really useful to make things work in the real world, especially for languages that you can distribute as single-file binaries (which IMO should be all of them, but sadly it's not always).

  • delduca 30 minutes ago
    My current workaround until it arrives in all C++ compilers

    ``` inline constexpr auto bootstrap = #include "bootstrap.lua" ;

    // ... later

    lua.script(bootstrap, "@bootstrap"); ```

    The lua code ``` R"( -- your code here )"; ```

  • mgaunard 1 hour ago
    surely the preprocessor method doesn't work in the general case, since the data can contain commas or parentheses.

    Regardless all of the methods suggested are terrible. If you don't have access to #embed, just write a trivial python script.

    • david2ndaccount 34 minutes ago
      You can apply `#` to __VA_ARGS__, which won’t preserve the exact whitespace, but for many languages it’s good enough. biggest issue is you can’t have `#` in the text.
    • oguz-ismail2 53 minutes ago
      How is `xxd -i' terrible?
      • mgaunard 39 minutes ago
        It's still lacking content that goes before/after the output.

        Just write a Python script that does the whole thing.

        • oguz-ismail2 34 minutes ago
          Don't know what you mean, it works fine here. Python is too large and unreliable a dependency for something so trivial (which can be accomplished using standard POSIX utilities if need be).
          • astrobe_ 10 minutes ago
            Indeed, even writing this utility in C is trivial and has 0 extra dependency for a pure C/C++ project. Avoiding #embed also removes the dependency to a C++23 capable compiler, which might not be available in uncommon scenarios.
          • jcalvinowens 14 minutes ago
            Python is pretty much mandatory for Linux systems nowadays, unless you're dealing with something really minimalist or trying to be very portable it's safe to rely on.
  • borcunozkablan 47 minutes ago
    Why don't you #embed?
    • qbow883 23 minutes ago
      Because the linked article is from 2013.