Intro

Compiled resources (platform-independent)

xxd GIMP

Linking (Windows-specific)

Linking icons as resources

RC file text RC to RES Linking the RES to your executable Resource access from code Using icons

Concluding thoughts

Runtime [.dll/.so files] - loaded with fopen and that family of functions.

I won't be going into this here as there are tutorials for them all over the internet and it's not relevant to my use case.

I won't be going into this here as there are tutorials for them all over the internet and it's not relevant to my use case. Linked [.lib/.a files] - processed and added by the linker, these have to be 'found' in the executable at runtime.

Compiled [files added with #include] - added to source code, commonly as an array of bytes in a header.

1 xxd test.geo

1 char Fifteen = 0x0F;

1 xxd -i test.geo test_geo.h

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 /* test_geo.h */ unsigned char test_geo [] = { 0 x47 , 0 x65 , 0 x6f , 0 x6d , 0 x65 , 0 x74 , 0 x65 , 0 x72 , 0 x01 , 0 x00 , 0 x06 , 0 x00 , 0 x49 , 0 xd9 , 0 x86 , 0 x56 , 0 x8b , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 xce , 0 xcc , 0 xec , 0 xc0 , 0 x04 , 0 x00 , 0 x00 , 0 xc0 , 0 x66 , 0 x35 , 0 xec , 0 x40 , 0 xbb , 0 x6d , 0 x6e , 0 xc1 , 0 xdc , 0 x4c , 0 xc0 , 0 xc1 , 0 xe2 , 0 x62 , 0 x46 , 0 xc1 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 x09 , 0 x21 , 0 x21 , 0 x02 , 0 x00 , 0 x00 , 0 x00 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x05 , 0 x00 , 0 x00 , 0 x00 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x02 , 0 x00 , 0 x00 , 0 x00 , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 x09 , 0 x00 , 0 x00 , 0 x00 , 0 xfa , 0 xff , 0 xff , 0 xff , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 xcd , 0 xcc , 0 x80 , 0 xc1 , 0 xcd , 0 xcc , 0 x4c , 0 x3e , 0 xfa , 0 xff , 0 xff , 0 xff , 0 x02 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x22 , 0 x84 , 0 xbf , 0 xb4 , 0 x39 , 0 x4f , 0 xc1 , 0 xfa , 0 xff , 0 xff , 0 xff , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 xf0 , 0 xb3 , 0 x3c , 0 x40 , 0 xdb , 0 x74 , 0 xc9 , 0 x40 , 0 x05 , 0 x00 , 0 x00 , 0 x00 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x02 , 0 x00 , 0 x00 , 0 x00 , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 x0a , 0 x00 , 0 x00 , 0 x00 , 0 xc0 , 0 xcc , 0 xcc , 0 x3e , 0 x67 , 0 x66 , 0 x06 , 0 x41 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x02 , 0 x00 , 0 x00 , 0 x00 , 0 x0a , 0 x00 , 0 x00 , 0 x00 , 0 xcd , 0 xcc , 0 x04 , 0 x41 , 0 x01 , 0 x00 , 0 x28 , 0 xc1 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x02 , 0 x00 , 0 x00 , 0 x00 , 0 x0a , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 xcd , 0 xcc , 0 xbd , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x02 , 0 x00 , 0 x00 , 0 x00 , 0 x0a , 0 x00 , 0 x00 , 0 x00 , 0 x34 , 0 x33 , 0 xaf , 0 xc1 , 0 x66 , 0 x66 , 0 xf6 , 0 xc0 , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x0a , 0 x00 , 0 x00 , 0 x00 , 0 x9a , 0 x99 , 0 x31 , 0 xc1 , 0 x34 , 0 x33 , 0 x63 , 0 xc1 , 0 x03 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x05 , 0 x00 , 0 x00 , 0 x00 , 0 x1a , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 xa0 , 0 x41 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x04 , 0 x00 , 0 x00 , 0 x00 , 0 x01 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x80 , 0 x3f , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 xcd , 0 xcc , 0 xcc , 0 x3d }; unsigned int test_geo_len = 419 ;

1 2 3 4 5 6 7 8 9 10 11 static const struct { unsigned int width; unsigned int height; unsigned int bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */ unsigned char pixel_data[700 * 400 * 4 + 1]; } gimp_image = { 700, 400, 4, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" /* ... */ };

Notate the files you want included in a .rc text file 'Compile' the .rc file to a .res file Link the .res file in with your program Access the resource data in your code Use the data

1 2 Icon ICON "G_32.ico" IconSmall ICON "G_16.ico"

1 rc -nologo geometer.rc

1 call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64

1 cl ...flags... win32_geometer.c /link ...libs... geometer.res

1 2 3 4 5 6 7 8 9 10 11 WNDCLASSEX WindowClass = {0}; WindowClass.cbSize = sizeof(WNDCLASSEX); WindowClass.style = CS_HREDRAW | CS_VREDRAW; WindowClass.lpfnWndProc = Win32MainWindowCallback; WindowClass.hInstance = Instance; WindowClass.hCursor = LoadCursor(0, IDC_ARROW); WindowClass.hIcon = LoadIcon(Instance, "Icon"); WindowClass.hIconSm = LoadIcon(Instance, "IconSmall"); WindowClass.lpszClassName = "BasicWindowClass";

At some point you'll probably want to include some data or resources into the executable itself (e.g. fonts, images etc). This is particularly true for my case, as I'm trying to keep to a portable single-executable model. That means that I can't keep resources in a surrounding folder and load at runtime, and I don't want to assume that the user already happens to have the files on their system.Icons are one example of a useful resource that takes an application from looking amateurish to looking like a Real Product (tm).This post will show some cross-platform methods of adding resources to your application, and a Windows-only method, with icons as an example.You can divide methods of including resources in your application into 3 groups based on when they are attached, each with an analogous form for adding libraries:Inherent in linking resources to your executable is that it will get larger. The resources that I'm using in an editor are few and small, and as such, the pros of portability and simplicity win out. If you're making a game with large textures and audio files, the trade-off may not be so appealing.The ease of modding resources in a folder layout is also something that might be more appreciated for a game.Adding resources via an array in a header file is the simplest in terms of runtime use: they can be accessed like any normal variable/array. As such, this is my preferred method unless there are major reasons to go a different way.xxd is a command-line utility for interacting with binary files, primarily converting to and from hex data. It's normally a unix application, but it also comes bundled with the Windows version of Vim , which is handy for me... (You can access it directly through Vim with :!xxd ... or find it in the application's folder.)The default output of xxd is what you see in a hex viewer, and is actually what I was using in my last blog post to validate my file format:Hey, this is just a bunch of hexadecimal numbers; you can format numbers like this in normal C code by slapping "0x" at the front:Why not just make an array of these and add them to the code?Helpfully for our purposes, xxd has an option to format its output for an include file:My test.geo file has changed since the hex dump image above, but here's a look at the current version.An array and a count of bytes - what more could you ask for!Note that the size on disc of your h file will be about 6 times larger than the file you're including - you're using 6 bytes to encode a single one: "0x47, ". I tend to search and replace to remove the spaces, making it about 5x the orginal filesize. This shouldn't be a problem unless you're really strapped for space on your development machine. It won't affect the executable size.I currently do this with a font. In one of my earlier releases I forgot that I was loading the font from disc at runtime, so the first feedback I got was that it was crashing on opening... not great!You can also do some compression to save space in the executable. You then just need a way to decompress at runtime If you change the data contents of your files frequently, they can be converted to C headers as part of your build process (before you compile).If you don't want to get xxd for whatever reason, there are alternatives out there , or you can make your own, which you can do pretty quickly.(Examples are from GIMP 2.8)While xxd outputs generic bytes from any old file type, if you want to easily include some images, GIMP has a nice feature where it will let you export C source code/headers in RGB(A). This is the basic format:(Note the more compact string-based encoding.)There are quite a few options, including basic run-length encoding, which dramatically decreases the size of images with blocks of colour. This also includes a macro for reading the RLE data.You can trade off a significant reduction in executable size with a slightly easier file to read. Have a play around with the various settings and see what you get.The other option we're exploring here is for the linker to bundle the file into the executable. Once it's there, we need a way to find it at runtime. On Windows, this is done using the 'Resource' API/system . The simplified process for this is:I'll work through this with icons as an example. It seems as though you have to include icons with this method to get them to show up everywhere you want.I'll assume here that you have an icon. If not, you can create and export .ico files with GIMP.RC file textThese have a pretty simple format: the name of the resource (as you'll access it in the code), the type, and the relative file location.There are a number of types, including a generic/custom data format.RC to RESThis just involves running the rc executable on the RC file. (The nologo flag just stops it from spitting out copyright information).This outputs geometer.res. If you're doing this from the command line then as with cl, you have to run the vcvarsall.bat in the shell before you run rc:Linking the RES to your executableThis is just linked as you would a static library:Resource access from codeTypical resources are accessed with FindResource and LoadResource . Some of these can be accessed more succinctly, e.g. LoadCursor and LoadIcon.Using iconsJust having the icon resource attached adds the icon to the executable and associated files in Windows Explorer/My Computer.Adding the icons to the top-left of the program window and the taskbar is easy if you're loading the window class yourself:I've shown how I include data in my executables on Windows and with a platform-independent method. I default to using the platform-independent method unless there's a good reason to use something else (like the special treatment icons get as 'Resources'). Other people I know just put everything in the resource files as they're only/primarily targeting Windows, and the RC files keeps everything in one place. There doesn't seem to be much difference in speed at compile- or run-time.Please do let me know if you attach files to your executable with these methods or if you have any related tips to share.