开发者

Platform independent resource management [duplicate]

This question already has answers here: Is there a Linux equivalent of Windows' "resource files"? 开发者_StackOverflow (2 answers) Closed 4 years ago.

I'm looking for a way to embed text files in my binaries (like windows resource system). I need something thats also platform independent (works in windows and linux). I found Qt resource management to be what I need but I'm not keen on my app depending on Qt for this alone. I also found this tool at http://www.taniwha.com/~paul/res/ .. but it is too platform specific.


The xxd utility can be used to create a C source file, containing your binary blobs as an array (with the -i command line option). You can compile that to an object which is linked into your executable.

xxd should be portable to most platforms.


If you're using QT 4.5, you can make sure that program is only dependent on one small piece of QT, such as libqtcore. QResource is a part of libqtcore.


You can simlpy append all kinds of data to your normal binary. Works in both Windows and Linux. You'll have to open your own binary at runtime and read the data from there.

However, I have to agree that embedding data in binaries is a strange idea. It's common practice to include such data as separate files packaged with the application.


That is not such a great idea. On Linux, for example, data is expected to be installed in a subdirectory of "$datadir" which is, by default, defined to be "$prefix/share", where "$prefix" is the installation prefix. On Mac OS X, resources are expected to be installed in $appbundle/Contents/Resources, where $appbundle is the name of the folder ending in ".app". On Windows, installing data in a folder that is a sibling of the executable is not an uncommon practice. You may be better off using the CMake build system, and using its CPack packaging features for installing/bundling in the default, preferred platform-specific manner.

Although bundling your resources into the executable, itself, may seem cool, it is actually a dangerous idea... for example, will the embedded data be allocated in an executable page? What will happen if you attempt to overwrite or modify the data? What if you want to tweak or modify the data at runtime? Things to think about.


This looks very promising: https://github.com/cyrilcode/embed-resource

CMake based and platform-independent.


As I also do not like the idea of converting files into C arrays only to have them converted back to binaries, I created my own resource compiler using LLVM and Clang:

https://github.com/nohajc/resman

I tested it on Windows, Linux and macOS but it can potentially be run on any platform supported by LLVM.

It is used like this:

  1. Create header file, e.g. res_list.h
#pragma once
#include "resman.h"

// Define a global variable for each file
// It will be used to refer to the resource
constexpr resman::Resource<1> gRes1("resource_file1.jpg"); // resource with ID 1
constexpr resman::Resource<2> gRes2("resource_file2.txt"); // resource with ID 2
constexpr resman::Resource<3> gRes3("resource_file3.mp3"); // resource with ID 3
...
  1. Run resource compiler

    $ rescomp res_list.h -o res_bundle.o

  2. Link res_bundle.o to your project

  3. Use the resource files
#include "res_list.h"
...
resman::ResourceHandle handle{gRes1};

// ResourceHandle provides convenient interface to do things like:

// iterate over bytes
for (char c : handle) { ... }

// convert bytes to string
std::string str{handle.begin(), handle.end()};

// query size and id
unsigned size = handle.size();
unsigned id = handle.id();

The resource compiler parses res_list.h (using Clang) but instead of generating cpp files, it goes straight to the native object file (or static library) format (using LLVM).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜