Resource management
Compiling external files into application binary.
The Utility::
Resource compilation
Imagine that you have e.g. a long license text with all these yelling words in all caps:
This application is copyright © John Doe et al. and the author IS NOT RESPONSIBLE AT ALL FOR ANY PHYSICAL OR OTHER DAMAGES AND INJURIES WHICH MAY LEAD TO DEATH OR EVEN APOCALYPSE AND CAN BE CAUSED BY THIS APPLICATION ...
And for obvious reasons you don't want to put directly into the code, have it in a separate file, but in the end you want it compiled directly into the executable, so your product is one compact file.
Corrade's CMake support provides the corrade_
find_package(Corrade REQUIRED Utility) set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON) corrade_add_resource(Resources resources.conf)
The configuration file contains group name (explained below) and list of all the files. The files can also have aliases for e.g. simplifying filenames.
group=data [file] filename=licenses/en.txt alias=license.txt [file] filename=icon.png
Resource management
Now it's time to make an application which will have these resources compiled in. The resources should be initialized automatically, if not (for example if they are compiled into static library), call CORRADE_main()
function.
This example application will just print the license text and exit:
#include <Corrade/Containers/StringView.h> #include <Corrade/Utility/Debug.h> #include <Corrade/Utility/Resource.h> using namespace Corrade; int main() { /* Initialize resource manager with name of the group we are using */ Utility::Resource rs{"data"}; /* Print out the license */ Utility::Debug{} << rs.getString("license.txt"); }
Compile the application with simple CMake add_executable()
command and don't forget to compile in the resource file created above:
add_executable(ResourceTest main.cpp ${Resources}) target_link_libraries(ResourceTest PRIVATE Corrade::Utility)
After successful compilation the application will print out the license text:
$ ./ResourceTest This application is copyright © John Doe et al. and the author IS NOT RESPONSIBLE AT ALL FOR ANY PHYSICAL OR OTHER DAMAGES AND INJURIES WHICH MAY LEAD TO DEATH OR EVEN APOCALYPSE AND CAN BE CAUSED BY THIS APPLICATION ...
The full file content is linked below. Full source code is also available in the GitHub repository.