Title: include binary data files into compiled binary without converting to hex arrays
Tags: c++;c;gcc
Question: I regularly want to include (compile in) binary data from external files as an uint8_t array into my programs (embedded or bare metal).
Is there a way without the extra step of converting them into a hex arrays?
Because this involves extra build steps that sometimes are not that easy to integrate e.g. in case of Arduino tool chain. And doing this manually always results in forgotten hex-updates :-)
Normally I am using gcc. So I also would be  happy if it is a gcc-specific pragma or another extension (although not that happy as with an generic solution).
maybe something like
static const __attribute__((rawDataFromFile("myDataFile.bin"))) uint8_t s_data[];
instead of the classical way to external
bin2hex myDataFile.bin myDataFile.hex
static const uint8_t s_data[] = {
#include "myDataFile.hex"
Solutions that work in C and in C++ would be preferred, but suggestions that only work in one of the two are also welcome.
Comment: There is a proposal to add `st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0embed` for a standardised way to embed arbitrary data in source files, without having to manually transcribe them into string literals or hex:
[p1040R0](http://open-std.org/JTC1/SC22/WG21/docs/papers/2018/p1040r0.html)

Comment: Not that I'm aware of. I'm interested as well, because I normally just use `xxd -i filename.bin` for my generations, which at least has the advantage of easily incorporating a dependency rule into a makefile.


Here is another answer: An old trick, which I used some 20 years ago with Turbo Pascal, of converting a binary file into an object one, came to my mind. Here it is with C++:
Data file data.dat:
Convert into an object file:
ld -r -b binary data.dat -o data.o
As explained here, with -b binary <name&gt; option the object file will contain raw binary data from the file <name&gt; in the .data section, and three symbols _binary_<name&gt;_start, _binary_<name&gt;_end, and _binary_<name&gt;_size will be created.
Simple program:
#include <cstdint&gt;
#include <iostream&gt;
extern const st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0uint8_t _binary_data_dat_start[];
extern const st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0uint8_t _binary_data_dat_end[];
int main()
for (auto d = _binary_data_dat_start; d != _binary_data_dat_end; ++d)
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << (char)*d << ' ';
Compilation and linking:
g++ -c test.cpp
g++ test.o data.o
Output:
Edit 1
that also introduces external preprocessing of the data and even introduces the need to know how the symbols will be named
Unfortunately, that's true. You might want to look at this library, which uses inline assembly to embed binary data.
Edit 2
Found a possible duplicate question.
Comment for this answer: +1 however it would be valuable to at least have the comment about the auto-generation of symbols in step 2, and what they'll look like in their program in step 3
Comment for this answer: @FMashiro, will do, thanks.
Comment for this answer: @vlad_tepesch, what about [this library](https://github.com/graphitemaster/incbin)?
Comment for this answer: thanks for answer, but that also introduces external preprocessing of the data and even introduces the need to know how the symbols will be named.
Comment for this answer: great, that did not came up on my search before. that library looks good on the first glance. I will have to double check if it is compatible with different target platform, since it uses inline assembly.

Here is another answer: use ld
ld -r -b binary binaryfile -o objectfile
objcopy --rename-section .data=.yoursection objectfile

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:11/12/19', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c++><c><gcc>', 'Title': 'include binary data files into compiled binary without converting to hex arrays', 'ViewCount': 1079, 'id': 58815959, 'user_AboutMe': '', 'user_AccountId': 2700834.0, 'user_DisplayName': 'vlad_tepesch', 'user_Reputation': 6485.0, 'user_Views': 728.0, 'user_id': 2331592.0}}