Live data from Hacker News

Embedding Binary Objects in C

flak.tedunangst.com

21–30 of 141 posts

Re: Embedding Binary Objects in C

#22
According to https://news.ycombinator.com/item?id=22865842 #embed proposal is in the review process http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2499.pdf . This similar (same?) proposal is also presented and in the review at WG21 to be included in C++ standard.

The author puts a lot of work towards making this proposal a reality (as far as I as a casual twitter/slack observer can see) and I'm looking forward to it.

Re: Embedding Binary Objects in C

#24

My favorite is simply using a tool to create a C file with your binary data: static uint8_t mydata[] = {0xDE, 0xAD, 0xBE, 0xEF, ... }; The advantage is that it works anywhere, with any compiler. The disadvantage is that it can increase compile time. I would limit each .c file to 10MB; it seems there's a quadratic increase in build time with file size, at least with gcc. Also, instead of "0x%02x" I use decimal notatio…

For those wondering, the easiest way to do this is with:

    xxd -i filename.bin

Re: Embedding Binary Objects in C

#25

If we're in the realm of "non-standard linker tricks"... Compilers will concatenate sections of the same name. You can use this trick to produce a concatenation of arrays across several files: $ cat t1.c __attribute__((section("some_array"))) int a[] = {1, 2, 3}; $ cat t2.c __attribute__((section("some_array"))) int b[] = {4, 5, 6}; $ cat t.c #include extern const int __start_some_array; extern const int __stop_some_…

nice

Re: Embedding Binary Objects in C

#27

My favorite is simply using a tool to create a C file with your binary data: static uint8_t mydata[] = {0xDE, 0xAD, 0xBE, 0xEF, ... }; The advantage is that it works anywhere, with any compiler. The disadvantage is that it can increase compile time. I would limit each .c file to 10MB; it seems there's a quadratic increase in build time with file size, at least with gcc. Also, instead of "0x%02x" I use decimal notatio…

if you do that you could also do base64 ;)

Re: Embedding Binary Objects in C

#29
post #27

My favorite is simply using a tool to create a C file with your binary data: static uint8_t mydata[] = {0xDE, 0xAD, 0xBE, 0xEF, ... }; The advantage is that it works anywhere, with any compiler. The disadvantage is that it can increase compile time. I would limit each .c file to 10MB; it seems there's a quadratic increase in build time with file size, at least with gcc. Also, instead of "0x%02x" I use decimal notatio…

if you do that you could also do base64 ;)

But you can’t? Base64 won’t be recognized by the C compiler, you’ll end up having to decode it at runtime.

Re: Embedding Binary Objects in C

#30
Using GNU assembly .inbbin is also quite flexible. And you can use .dc.* and .asciz and .byte to add arbitrary data inline.

Generating assembly files is my favorite way of including outside data into my C programs.

Post reply on HN