Search code examples
c++cgcclinkerembedded-resource

Can I link a plain file into my executable?


Some frameworks (Qt, Windows, Gtk...) offer functionality to add resources to your binaries. I wonder if it would be possible to achieve this without the framework, since all that is really needed is

  1. a symbol to contain the resource's address within the binary (data segment)
  2. a symbol to represent the length of the resource
  3. the resource itself

How can this be achieved with the gcc toolchain?


Solution

  • You could do this:

    objcopy --input binary \
            --output elf32-i386 \
            --binary-architecture i386 my_file.xml myfile.o
    

    This produces an object file that you can link into your executable. This file will contain these symbols that you'll have to declare in your C code to be able to use them

    00000550 D _binary_my_file_xml_end
    00000550 A _binary_my_file_xml_size 
    00000000 D _binary_my_file_xml_start