久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 GCC 在可執行文件中嵌入資源

Embedding resources in executable using GCC(使用 GCC 在可執行文件中嵌入資源)
本文介紹了使用 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.

我想做的一個很好的例子是處理著色器代碼 - 我可以將它保存在源文件中,例如 const char* shader = "source here"; 但這非常不切實際.

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"并將其內容鏈接到我的程序,以便我能夠以二進制形式訪問內容代碼中的數據.

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 的功能將任何文件轉換為對象(嵌入二進制 blob使用 gcc mingw):

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

  • 使用 bin2c/bin2h 實用程序將任何文件轉換為字節數組 (在代碼中嵌入圖片,不使用資源部分或外部圖片)

  • 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 binary綁定到可執行文件中的數據:

    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
    ", &_binary_foo_bar_start);
        printf( "address of end: %p
    ", &_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 通過使用 display (unsigned int)&_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 在可執行文件中嵌入資源的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
  • 相關文檔推薦

    Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
    How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
    When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
    Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
    Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
    Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
    主站蜘蛛池模板: 亚洲成人国产 | 国产黄色在线播放 | 性欧美bbw| 欧美又大又硬又粗bbbbb | 毛片毛片毛片毛片毛片 | 欧美视频亚洲视频 | 国产成人一区二区 | 国产高清av | 国产精品美女在线观看 | 日本在线不卡视频 | 伊人亚洲综合 | 午夜激情在线观看 | 九九热在线观看视频 | aaa黄色| 在线看黄色片 | 国产精品伦子伦免费视频 | 欧美成人猛片aaaaaaa | 亚洲视频国产 | 一区二区三区成人 | 欧美性受xxxx黑人xyx性爽 | 成人免费av | 成人做爰免费视频免费看 | 欧美日韩成人在线观看 | 黄色一级大片在线免费看国产一 | 久久久久免费 | 野外(巨肉高h)| 日本不卡一区 | 欧美一区二区免费 | 在线观看网址你懂的 | 免费的黄色网址 | 日韩欧美黄色 | 欧美vieox另类极品 | 国产黄a三级三级三级看三级男男 | 一区二区三区黄色 | 一区二区在线视频 | 国产精品久久久久久中文字 | 亚洲高清视频在线观看 | 伊人影院久久 | 成人午夜网 | 日韩小视频在线观看 | 好好的日com |