Advertisement Jump to content


This topic is now archived and is closed to further replies.


Embedded resources in executables on non-Windows OSes

This topic is 5463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know resources can be added to the end of Windows executables and accessed by the program without problems. Can you do the same thing under non-Windows OSes like Linux and MacOS?

Share this post

Link to post
Share on other sites
Erm, well, not exactly sure what you''re asking, but it''s very possible to include resources as part of the executable (although not necessairly smart as it makes upgrading changes larger than necessary (replacing the entire exe) or more complicated than necessary (requiring patchfiles)).

strings in C/C++ for example ("pie" "fnord" etc) are stored inside the exe (or other executable format) automatically.

You can load variable constants like these from the linux equivilant of dll files (.so files) by using dlopen/dlsym/etc... so it''d be very possible to do it that way.

I''m not aware of any tools for implementing this (although there might be) but the simplest way that I can think of would be to create a program/script that outputs a dummy .c file from any file which creates a variable... something like:

#!/usr/bin/php -q
if ($argc != 2) {
echo "usage: create_resource.php [ resource_name ] < input > output [ 2> error_log ]";
$resname = $argv[1];
echo "const char $resname[] = \n";
while (!feof(STDIN)) {
echo ''"'';
$data = fread(STDIN,8192);
echo addcslashes($data,"\0..\377"); //escapes everything
echo ''"\n'';
echo '';'';

note that the above script was written in php, and after compiling the generated code you will have a variable named resource_name, of type (const char[]), with 1 extra byte added to the resource (null terminator 0). Aka, using this on a 4 byte file would after compilation produce a variable with 5 bytes of data. You wouldn''t want to use strlen on this variable as binary data could contain 0s... but you can use sizeof()-1.

You could also use an unsigned char, and have the script generate output like: {255,12,56,39,11,18} instead of escaping them in a string.

Use a custom makefile entity and you''re set (aka:

%.res.c : %.res
restocres.php < $< > $@
resources : $(patsubst %.res,%.res.o,$(wildcard *.res))

the first rule generates .res.c files from .res files, and the second rule is a target, relying on a .o file for each .res file (which the compiler will make from the corresponding .c with it''s own predefined rule).

The script could do much better written in a language such as C. But I''m lazy and simply explaining possibilities anyways.

Share this post

Link to post
Share on other sites
Embedded resources predate Windows. Old DOS games did it with regularity. Find a hex editor, include large-ish data segments in your executable (an zeroed static array for instance) and use the hex editor (or a custom tool) to transfer the data into the data segment(s). You''d need code that would open the executable and retrieve said data itself.

I''m sure such programs exist. I''d be really surprised if none were out there.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!