Martin
Martin

Reputation: 107

Include binary file as array of constants

I have an embedded C project I am compiling using eclipse. I need to read a binary file into the application code as an array of constants.

This binary file is ~200kB and needs to be part of the application code so that the application code can read the binary image at anytime and load it into another device on the board that needs this initialization image.

I would normally load the image into a non-volatile memory on the board, then read it and move it, but that is not feasible here, it has to be part of the executable image.

I can do this in the makefile by linking the .bin file to a certain address, or in the C code something like

const char binFileImage [] = { file.bin };

This obviously does not work, but I have not come up with syntax that would work.

FYI, the file.bin really is a binary file.

Any thoughts on how to do this?

Upvotes: 4

Views: 8400

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

Using a linker script to put the binary file in a specific address is probably the best and simplest solution.

Other solutions include using some program that is called by the makefile to convert the file into a source file containing a valid array definition. For example, lets say the file starts with the values 0x23, 0x05, 0xb3 and 0x8f, then the automatically generated source file could look something like

const uint8_t binary_file_data[] = {
    0x23, 0x05, 0xb3, 0x8f, ...
};

Upvotes: 5

Related Questions