Catnap Games

Android-like resource linking in C++

Here's something that is actually quite nice about Android development: the linking of resources into source code. There is an extra build step that takes all the resource files in your project and generates a class called R which contains references to those files.

For example, you can add icon.png and then use R.drawable.icon in your source code to load it.

I decided to do something similar for my C++ project. I have made a small demo and put it on GitHub.


A simple example of creating Android-like enumeration of resource files that allows easier referencing from source code.

GitHub project here (Xcode project)

What it does

At build time, it gets a list of all .png and .ogg files in the Resources directory and generates a header file that contains a Resources class with the following structure:

Resources::Images::Count - number of images found Resources::Images::List - a NULL terminated array containing the filenames Resources::Images::* - each individual file is contained as a char* variable

Same goes for Resources::Sounds and any other group of files you choose to include (just edit the script).


  1. Reference checks at compile time, not runtime.
  2. Autocomplete for resource names in the code editor.

Reference checks at compile time, not runtime

Autocomplete for resource names in the code editor

How it works in Xcode

There's an extra "Run script" build step added to the project which runs That generates a new version of Resources.h. Note that Resources.cpp is also required but it doesn't change very often so for this example I wrote it by hand. - I admit - is a bit of a mess, but it works and it should be easy to add more file types.