I wouldn't do it this way, within the toolchain. Better to just append the data to the finished executable, with a header that contains an identifying marker. The program can scan its own image and look for that marker to get at the data. That will work on any OS with any executable and linker format and can be done post-production (users or downstream distributors can receive the binary and add customized data to it…
Embedding Binary Objects in C
51–60 of 141 posts
Re: Embedding Binary Objects in C
#52Re: Embedding Binary Objects in C
#53Re: Embedding Binary Objects in C
#54My 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…
Re: Embedding Binary Objects in C
#55Re: Embedding Binary Objects in C
#56Re: Embedding Binary Objects in C
#57This is simply the include_bytes! macro in Rust. There's also the include_str! macro which does a compile-time check for UTF-8 validity.
Re: Embedding Binary Objects in C
#58There's also gnu's objcopy. This post covers a lot of different approaches, and has lots of tips: https://www.devever.net/~hl/incbin
One of us from Memfault also wrote a post about tips with Binutils, and this approach is covered in it.
Re: Embedding Binary Objects in C
#59 const a = readFile("mydata")
The `const` makes the expression evaluate at compile time, conveniently slurping the file into `a`, where it will be available at run time.Re: Embedding Binary Objects in C
#60Unfortunately, this technique is a bit problematic with modern C compilers. Because the `start` and `end` symbols are unrelated objects, as far as the compiler is concerned, the subtraction `&end - &start` to get the length of the data invokes undefined behavior. Just for that reason, I feel the include file with a hex dump is the better method.