Does anybody have an idea how to statically compile any resource file right into the executable or the shared library file using GCC?
For example I'd like to add image files that never change (and if they do, I'd have to replace the file anyway) and wouldn't want them to lie around in the file system.
If this is possible (and I think it is because Visual C++ for Windows can do this, too), how can I load the files which are stored in the own binary? Does the executable parse itself, find the file and extract the data out of it?
Maybe there's an option for GCC which I haven't seen yet. Using search engines didn't really spit out the right stuff.
I would need this to work for shared libraries and normal ELF-executables.
In the menu, go to Project > Add New Item. Select the Visual C++ folder and choose Resource File (. rc) in the right pane. Provide a name for your resource script file in the Name text box and select Open.
Linking is performed when the input file are object files " .o " (instead of source file " . cpp " or " . c "). GCC uses a separate linker program (called ld.exe ) to perform the linking.
Update I have grown to prefer the control John Ripley's assembly .incbin
based solution offers and now use a variant on that.
I have used objcopy (GNU binutils) to link the binary data from a file foo-data.bin into the data section of the executable:
objcopy -B i386 -I binary -O elf32-i386 foo-data.bin foo-data.o
This gives you a foo-data.o
object file which you can link into your executable. The C interface looks something like
/** created from binary via objcopy */
extern uint8_t foo_data[] asm("_binary_foo_data_bin_start");
extern uint8_t foo_data_size[] asm("_binary_foo_data_bin_size");
extern uint8_t foo_data_end[] asm("_binary_foo_data_bin_end");
so you can do stuff like
for (uint8_t *byte=foo_data; byte<foo_data_end; ++byte) {
transmit_single_byte(*byte);
}
or
size_t foo_size = (size_t)((void *)foo_data_size);
void *foo_copy = malloc(foo_size);
assert(foo_copy);
memcpy(foo_copy, foo_data, foo_size);
If your target architecture has special constraints as to where constant and variable data is stored, or you want to store that data in the .text
segment to make it fit into the same memory type as your program code, you can play with the objcopy
parameters some more.
With imagemagick:
convert file.png data.h
Gives something like:
/*
data.h (PNM).
*/
static unsigned char
MagickImage[] =
{
0x50, 0x36, 0x0A, 0x23, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20,
0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4D, 0x50, 0x0A, 0x32, 0x37,
0x37, 0x20, 0x31, 0x36, 0x32, 0x0A, 0x32, 0x35, 0x35, 0x0A, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
....
For compatibility with other code you can then use either fmemopen
to get a "regular" FILE *
object, or alternatively std::stringstream
to make an iostream
. std::stringstream
is not great for this though and you can of course just use a pointer anywhere you can use an iterator.
If you're using this with automake don't forget to set BUILT_SOURCES appropriately.
The nice thing about doing it this way is:
You can embed binary files in executable using ld
linker.
For example, if you have file foo.bar
then you can embed it in executable adding the following commands to ld
--format=binary foo.bar --format=default
If you are invoking ld
thru gcc
then you will need to add -Wl
-Wl,--format=binary -Wl,foo.bar -Wl,--format=default
Here --format=binary
tells the linker that the following file is binary and --format=default
switches back to default input format (this is usefull if you will specify other input files after foo.bar
).
Then you can access content of your file from code:
extern uint8_t data[] asm("_binary_foo_bar_start");
extern uint8_t data_end[] asm("_binary_foo_bar_end");
There is also symbol named "_binary_foo_bar_size"
. I think it is of type uintptr_t
but i didn't check it.
You can put all your resources into a ZIP file and append that to the end of the executable file:
g++ foo.c -o foo0
zip -r resources.zip resources/
cat foo0 resources.zip >foo
This works, because a) Most executable image formats don't care if there's extra data behind the image and b) zip stores the file signature at the end of the zip file. This means, your executable is a regular zip file after this (except for your upfront executable, which zip can handle), which can be opened and read with libzip.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With