久久久久久久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)嗎?)
    主站蜘蛛池模板: 精品一级 | 国产ts人妖一区二区三区 | 成人动漫视频网站 | 国产高清精品在线 | 久久久综合网 | 日韩免费视频 | 玖玖在线免费视频 | 国产一区二区视频免费在线观看 | 国产精品久久午夜夜伦鲁鲁 | 国产亚洲精品精品国产亚洲综合 | 久久久久综合 | 日本在线免费看最新的电影 | 日韩高清在线观看 | 在线日韩欧美 | 国产欧美精品一区二区 | 亚洲精品在线观看网站 | www.青娱乐| 国产成人精品免费视频大全最热 | 精品欧美一区二区三区久久久 | 在线欧美视频 | 日本一区二区三区视频在线 | 中文字幕高清免费日韩视频在线 | 国产98色在线 | 日韩 | 第四色影音先锋 | 日韩精品视频网 | 久久精品久久精品久久精品 | 视频一区二区在线 | 免费精品久久久久久中文字幕 | 天天拍天天草 | 亚洲精品国产成人 | 午夜影院在线观看视频 | 久久免费福利 | 精品国产31久久久久久 | 成人免费在线播放 | 中国av在线免费观看 | 久久免费视频2 | 一级做a爰片性色毛片16美国 | 欧美精品一二区 | 国产精品射| 欧美一区二区三区在线播放 | 久久天天|