0

I would like to include the content of some binary files in a statically linked C++ project - for context, these are payloads to be sent to vendor hardware devices.

Consequently I would like to end up at compile time with the content and length of files (whose paths are determined at compile time) in vector<uint8_t>s and size_ts, for instance.

What's the best way to do this ?

For the record, a fairly similar question was asked here, however, while there's always the option of using a script to convert each byte of the binary file to its numeric representation so I can later include it as part of a generated source code file, I was hoping there may be common support for some more natural way to do this.

Thanks.

Julien BERNARD
  • 653
  • 6
  • 17
  • You can use `#embed`, q.v. https://stackoverflow.com/a/73570472/4641116 – Eljay Sep 02 '22 at 21:04
  • Damn, that seems to be C-specific. Though I guess I could codegen C and include it in my C++ project. Either way it seems my question is a duplicate of that one, that I hadn't found. Thanks for the link ! – Julien BERNARD Sep 02 '22 at 21:06
  • There is [`std::embed` proposal](https://open-std.org/JTC1/SC22/WG21/docs/papers/2020/p1040r6.html) – Jarod42 Sep 02 '22 at 21:18
  • From what I'm reading, it's not going well... https://thephd.dev/full-circle-embed – Julien BERNARD Sep 02 '22 at 21:19
  • This is how to include a binary file in your C++ program, consisting of three bytes, 1, 2, and 3: `static const char binaryfile[3]={0x01,0x02,0x03};`. That's my binary file. This will work for binary file of any size. – Sam Varshavchik Sep 02 '22 at 21:22
  • If you can't use `#embed`, you might be able to use `xxd -i foo.bin` to convert your binary file into a byte array, and then compile-and-link that object code into your file. That's one way I get binary data into my application's executable. – Eljay Sep 02 '22 at 22:22

0 Answers0