带有 GCC 的 C/C++:静态地将资源文件添加到可执行文件/库 [英] C/C++ with GCC: Statically add resource files to executable/library

查看:29
本文介绍了带有 GCC 的 C/C++:静态地将资源文件添加到可执行文件/库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用 GCC 将任何资源文件静态编译成可执行文件或共享库文件吗?

Does anybody have an idea how to statically compile any resource file right into the executable or the shared library file using GCC?

例如,我想添加永远不会更改的图像文件(如果更改,我无论如何都必须替换该文件)并且不希望它们位于文件系统中.

For example I'd like to add image files that never change (and if they do, I'd have to replace the file anyway) and wouldn't want them to lie around in the file system.

如果这是可能的(我认为这是因为 Visual C++ for Windows 也可以这样做),我如何加载存储在自己的二进制文件中的文件?可执行文件是否解析自身、找到文件并从中提取数据?

If this is possible (and I think it is because Visual C++ for Windows can do this, too), how can I load the files which are stored in the own binary? Does the executable parse itself, find the file and extract the data out of it?

也许我还没有看到 GCC 的选项.使用搜索引擎并没有真正吐出正确的东西.

Maybe there's an option for GCC which I haven't seen yet. Using search engines didn't really spit out the right stuff.

我需要它来处理共享库和普通的 ELF 可执行文件.

I would need this to work for shared libraries and normal ELF-executables.

推荐答案

With imagemagick:

convert file.png data.h

给出类似:

/*
  data.h (PNM).
*/
static unsigned char
  MagickImage[] =
  {
    0x50, 0x36, 0x0A, 0x23, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 
    0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4D, 0x50, 0x0A, 0x32, 0x37, 
    0x37, 0x20, 0x31, 0x36, 0x32, 0x0A, 0x32, 0x35, 0x35, 0x0A, 0xFF, 0xFF, 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 

....

为了与其他代码兼容,您可以使用 fmemopen 来获取常规"FILE * 对象,或者使用 std::stringstream> 制作一个 iostream.std::stringstream 对此并不好,你当然可以在任何可以使用迭代器的地方使用指针.

For compatibility with other code you can then use either fmemopen to get a "regular" FILE * object, or alternatively std::stringstream to make an iostream. std::stringstream is not great for this though and you can of course just use a pointer anywhere you can use an iterator.

如果您在 automake 中使用它,请不要忘记 适当地设置 BUILT_SOURCES.

If you're using this with automake don't forget to set BUILT_SOURCES appropriately.

这样做的好处是:

  1. 你得到文本,所以它可以在版本控制和补丁中明智地
  2. 它在每个平台上都具有便携性和良好定义

这篇关于带有 GCC 的 C/C++:静态地将资源文件添加到可执行文件/库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆