Is there a way to load a binary as a constant variable in C at compile time

I was wondering if there is a way to load an external binary as a variable in C via include or a header file or something similar.

For example, in the project I'm currently working on, I work with an embedded system that has a graphic display that will make text and small graphics (boxes, lines, etc.) using data and ASCII commands. But it will also display monochrome bitmap images. Therefore, I have a series of static displays that I use for the user interface, and several bitmaps for splash screens.

Now the reason I mention that this is an embedded system is because there is no file system to load data from, only from RAM and program memory, so any "preliminary" data or tables that I want to use should be loaded to compile time either through the source file or through the object file using the linker. Unfortunately, the IDE does not provide any means to load a binary file in any form into program memory for use as a data cache in this way in any easily recognizable way.

So I'm not doing what I already need to get around (use the hex editor to read the binary as encoded ASCII hex code and copy and paste the raw data into the header file as a variable), is there a way to β€œlink” to the file or "include" a file that can be loaded as a const variable at compile time?

The system I'm working with is MPLAB X for Microchip processors, and the GNC compiler. Basically, I want to know if there is a way to do this with some C command or function, before I try to find a way specifically using my specific compiler / linker software.

+6
source share
3 answers

No, you cannot do this directly. One way is to convert your binaries to C source code and include these snippets in your project. The conversion can be done using a simple program written by you or some third-party program.

For instance:

Binaray1.c (automatically generated)

unsigned char binaray_file1[] = { 1,2,3,10,20, .... } ; 

Binaray2.c (automatically generated)

 unsigned char binaray_file2[] = { 0,0,10,20,45,32,212, .... } ; 

SomeSourceFile.c

 extern unsigned char binary_file1[] ; extern unsigned char binary_file2[] ; // use binary_file1 and binary_file2 here. 
+5
source

The usual way to do this on Unix-like systems is to use ld -r binary . Here's a tutorial for Linux: http://www.burtonini.com/blog/computers/ld-blobs-2007-07-13-15-15-50

And one for Mac OS X, which is a bit more complicated: Compile a binary for OSX binding

The idea is for the linker to create an object file with known symbol names that point to the beginning and end of the binary blob that it copies to the resulting object file. Then you link this object file to your application and refer to blob using extern char* pointers or so.

I assume that you know how to associate objects with your system, so the remaining question is whether your linker supports something like -r binary .

+4
source

Of course, you could come up with your own format, but before you do this: look at the XPM bitmap format , which is specifically designed for this purpose: Integrating pixmaps into C.

0
source

All Articles