问题
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.
回答1:
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.
回答2:
The xxd
answer to this question is what you're looking for.
回答3:
I have just patched them onto the end of the executable at link time as a binary blob. With the last 4bytes being the size of the previous block and then have the program read the data items from the tail.
Another approach if you need a more filesystem type structure (though I havem't tried it) would be to put everything in a zip file and append that to the end. Again you would need some easily findabale size of the added data.
回答4:
We're using ICU ResourceBundles for that and are pretty satisfied with it.
Using the pkgdata tool, packageing of ResourceBundles is pretty flexible: as a shared library, static library, or as files that can be memory-mapped by ICU.
来源:https://stackoverflow.com/questions/1906265/platform-independent-resource-system-like-the-qt-resource-system