Is there a platform independent resource system for C++ like the one that comes with Qt (but without the Qt dependency)?
I would like to access arbitrary data from within my C++ source code. That is, not only icons but also translations or shaders, etc.
Alternatively some sort of virtual file system library to access e.g. a ZIP compressed file or such would also fit my needs.
The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files.
The rcc tool is used to embed resources into a Qt application during the build process. It works by generating a C++ source file containing data specified in a Qt resource (. qrc) file.
I rolled my own system for a C++ web server project that basically took a bunch of files (HTML, CSS, JS, PNGs, etc.) and created C++ headers containing the data encoded as static const char*
. I then #include
those headers where I need access to the data. The app that encodes the 'resource' files executes as a pre-build step. The encoding app itself used boost::filesystem
to create the resource headers, so works on Windows/*nix.
A typical resource file might look like this:
namespace resource
{
// Generated from mainPage.htm
static const char* mainPage_[] =
{
"<html>...</html>"
};
}
For binary content I encode using the \x
notation. I also make sure to line-wrap the data so it is readable in an editor.
I did have some issues though - the MS compiler doesn't allow a static const char*
to be bigger than 64Kb which was a PITA. Luckily the only files larger than this were JavaScript files that I could easily split into smaller chunks - large images would be a problem though.
The xxd
answer to this question is what you're looking for.
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