在可执行文件中嵌入资源使用GCC [英] Embedding resources in executable using GCC

查看:85
本文介绍了在可执行文件中嵌入资源使用GCC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种能够方便地通过GCC编译一个C / C ++应用程序中嵌入任何外部的二进制数据。

I'm looking for a way to easily embed any external binary data in a C/C++ application compiled by GCC.

的想什么,我做的是处理着色器code一个很好的例子 - 我只是不停地在源文件,如为const char *着色器=源这里; 但是这是非常不切实际的。

A good example of what I'd like to do is handling shader code - I can just keep it in source files like const char* shader = "source here"; but that's extremely impractical.

我想编译器为我做的:在编译时(联阶段),读取文件foo.bar​​,其内容链接到我的程序,让我能够访问的内容为二进制从code数据。

I'd like the compiler to do it for me: upon compilation (linking stage), read file "foo.bar" and link its content to my program, so that I'd be able to access the contents as binary data from the code.

可能是我想分发作为一个单一的.exe文件,其中小应用。

Could be useful for small applications which I'd like to distribute as a single .exe file.

GCC是否支持这样的事情?

Does GCC support something like this?

推荐答案

有几个可能性:

ld -r -b binary -o binary.o foo.bar  # then link in binary.o


  • 使用 bin2c / bin2h 实用工具将所有文件转换成字节数组(<一个href=\"http://stackoverflow.com/questions/225480/embed-image-in-$c$c-without-using-resource-section-or-external-images/225658#225658\">Embed在code图像,而无需使用资源部分或外部图像)

  • use a bin2c/bin2h utility to turn any file into an array of bytes (Embed image in code, without using resource section or external images)

    更新:这里有一个如何使用来使用绑定到可执行的数据更完整的示例LD -r -b二进制

    Update: Here's a more complete example of how to use data bound into the executable using ld -r -b binary:

    #include <stdio.h>
    
    // a file named foo.bar with some example text is 'imported' into 
    // an object file using the following command:
    //
    //      ld -r -b binary -o foo.bar.o foo.bar
    //
    // That creates an bject file named "foo.bar.o" with the following 
    // symbols:
    //
    //      _binary_foo_bar_start
    //      _binary_foo_bar_end
    //      _binary_foo_bar_size
    //
    // Note that the symbols are addresses (so for example, to get the 
    // size value, you have to get the address of the _binary_foo_bar_size
    // symbol).
    //
    // In my example, foo.bar is a simple text file, and this program will
    // dump the contents of that file which has been linked in by specifying
    // foo.bar.o as an object file input to the linker when the progrma is built
    
    extern char _binary_foo_bar_start[];
    extern char _binary_foo_bar_end[];
    
    int main(void)
    {
        printf( "address of start: %p\n", &_binary_foo_bar_start);
        printf( "address of end: %p\n", &_binary_foo_bar_end);
    
        for (char* p = _binary_foo_bar_start; p != _binary_foo_bar_end; ++p) {
            putchar( *p);
        }
    
        return 0;
    }
    


    更新2 - 获取资源大小:我无​​法正确读取_binary_foo_bar_size。在运行时,GDB显示我用显示(无符号整数)及文本资源的大小合适; _binary_foo_bar_size 。不过给一个变量赋值这​​总是给一个错误的值。我可以解决这个问题的方式如下:


    Update 2 - Getting the resource size: I could not read the _binary_foo_bar_size correctly. At runtime, gdb shows me the right size of the text resource by using display (unsigned int)&_binary_foo_bar_size. But assigning this to a variable gave always a wrong value. I could solve this issue the following way:

    unsigned int iSize =  (unsigned int)(&_binary_foo_bar_end - &_binary_foo_bar_start)
    

    这是一个解决办法,但它的作品好,是不是太难看。

    It is a workaround, but it works good and is not too ugly.

    这篇关于在可执行文件中嵌入资源使用GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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