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

使用 GCC 在可執(zhí)行文件中嵌入資源

Embedding resources in executable using GCC(使用 GCC 在可執(zhí)行文件中嵌入資源)
本文介紹了使用 GCC 在可執(zhí)行文件中嵌入資源的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在尋找一種方法,可以在 GCC 編譯的 C/C++ 應(yīng)用程序中輕松嵌入任何外部二進(jìn)制數(shù)據(jù).

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

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

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.

我希望編譯器為我做這件事:在編譯(鏈接階段)時(shí),讀取文件foo.bar"并將其內(nèi)容鏈接到我的程序,以便我能夠以二進(jìn)制形式訪問內(nèi)容代碼中的數(shù)據(jù).

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.

對(duì)于我想作為單個(gè) .exe 文件分發(fā)的小型應(yīng)用程序可能很有用.

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

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

  • 使用 bin2c/bin2h 實(shí)用程序?qū)⑷魏挝募D(zhuǎn)換為字節(jié)數(shù)組 (在代碼中嵌入圖片,不使用資源部分或外部圖片)

  • 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)

    更新:這里有一個(gè)更完整的例子,說明如何使用ld -r -b binary綁定到可執(zhí)行文件中的數(shù)據(jù):

    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;
    }
    

    <小時(shí)>

    更新 2 - 獲取資源大小:我無法正確讀取 _binary_foo_bar_size.在運(yùn)行時(shí),gdb 通過使用 display (unsigned int)&_binary_foo_bar_size 向我顯示文本資源的正確大小.但是將其分配給變量總是給出錯(cuò)誤的值.我可以通過以下方式解決這個(gè)問題:


    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.

    這篇關(guān)于使用 GCC 在可執(zhí)行文件中嵌入資源的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

    Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
    How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
    When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
    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 仍然相關(guān)嗎?)
    主站蜘蛛池模板: 香蕉大人久久国产成人av | 亚洲成人中文字幕 | 美女爽到呻吟久久久久 | 国产永久免费 | 精品中文字幕一区二区 | 免费毛片网站在线观看 | 成人妇女免费播放久久久 | 亚洲精品久久久久久久久久久久久 | 色综合激情 | 午夜视频网站 | 国产成人精品一区二区三区四区 | 国产亚韩 | 极品一区 | 九色国产 | 久久天天躁狠狠躁夜夜躁2014 | 操亚洲| 久久婷婷国产麻豆91 | 国产精品免费一区二区 | 久久久精品网 | av喷水| 国产精品欧美日韩 | 亚洲第一av网站 | 国产剧情一区二区三区 | 国产精品久久影院 | 日韩成人中文字幕 | 国产美女精品视频免费观看 | 一区二区三区国产好 | 亚洲第一区国产精品 | 久久国产一区二区三区 | 中文字幕人成乱码在线观看 | 亚洲欧美成人 | 日韩欧美在线播放 | av在线一区二区三区 | 国产成人精品免费 | 国产在线视频在线观看 | 亚洲欧美日韩精品久久亚洲区 | 中文字幕在线一区 | 亚洲成人一二区 | 国产一区在线免费观看 | 国产一区二区中文字幕 | 一区二区三区视频在线观看 |