Applications usually load resources like images from disk. But, in some situations, it may be better to embed binary data right into the executable file. Here are a few ways to do that.
In language like C and C++, the straight-forward way to include binary data in the executable is to convert it to a character array:
While there's nothing wrong with that approach, it requires a tool to convert
binary data to code (for example,
xxd -i). Besides, I find it a bit
inelegant, and I'll present some alternatives for you.
This is by far the easiest solution — but, unfortunately, doesn't work on Mac OS X. It does for Linux, though!
Let's say you have an image
cat.png and want to embed it into your
application. You can create an object file with
The object file will have three symbols in it,
(That's not actual output, but gives you an idea on how to see the symbols. You
can also use
objdump -x cat.o).
To use them from C, declare some extern variables
cat.o to the compiler:
If you have a function
display_png_image, you can simply call
If you don't have a working GNU
ld — or, if you want utter and complete
control — you can use an assembler like nasm.
Just use the
incbin directive to include the data. Add some helper symbols
for the end of the data, and use a macro to calculate the byte length:
On OS X, compile with
Finally, link your program exactly as before:
So what's so good about this approach? It lets you put the binary data in the
read-only data section, meaning it will be truly read-only. If you used
instead, you'd have to use
objcopy to move the symbols to the
You can also use
objcopy from the GNU binutils package. However, I wasn't
able to get it working completely on OS X. Meaning, there isn't an easy,
cross-platform way to use it, so I won't write much about it.
But, you can start by doing something like
On my system, the linker didn't like the resulting object file. I'll post an update if I get it working. On Linux, it should be straight-forward.
Most statically compiled languages will let you link in object files. I haven't tried, but I guess you could easily do it in languages like Swift, Rust and so on.
Of course, you can embed stuff like images and music into your application. For desktop applications, this may be an advantage in certain situations. But there are other cool uses as well.
Mike Pall, the original author of LuaJIT, gives an example where he compiles Lua programs to object files and wrap them up in an archive file. He then proceeds to link a host program in C with them, so the scripts can be executed without loading anything from disk.
I'm sure there are many other use cases as well.