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

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

问题描述

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



例如,我想添加图片文件从不改变(如果他们这样做,我必须替换文件),并且不希望它们在文件系统中。



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



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



我需要这个工作在共享库和正常的ELF可执行文件。



感谢任何帮助

解决方案

使用 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 c $ c> iostream 。 std :: stringstream 不是很好的,你可以只使用一个指针,你可以使用迭代器。



如果您在automake中使用此方法,请不要忘记设置BUILT_SOURCES



这样做的好处是:






  1. 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 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.

    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?

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

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

    Any help is appreciated

    解决方案

    With imagemagick:

    convert file.png data.h
    

    Gives something like:

    /*
      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, 
    
    ....
    

    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.

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

    The nice thing about doing it this way is:

    1. You get text out, so it can be in version control and patches sensibly
    2. It is portable and well defined on every platform

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

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