Include binary file?

Joel Dice joel.dice@gmail.com
Thu May 20 18:39:00 GMT 2010


On Thu, 20 May 2010, Mark Ainsworth wrote:

> Hello, at link time I would like to include the contents of a binary file 
> into the output executable, in a separate segment.  In my code I would then 
> read this as an array.  I've done this before with other compilers / linkers, 
> but not with gcc.  Can you show me the directives to do this?

You can use objcopy (part of binutils) to do this.  For example:

   objcopy -I binary image.png -O elf64-x86-64 -B i386:x86-64 image_png.o

You can then access the contents of image.png like so:

   extern const uint8_t _binary_image_png_start[];
   extern const uint8_t _binary_image_png_size[];

   const uint8_t*
   get_image_png(unsigned* size)
   {
     *size = (uintptr_t) _binary_image_png_size;
     return _binary_image_png_start;
   }

Hope that helps.



More information about the Gcc-help mailing list