pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問(wèn)題描述

我正在尋找一種方法,可以在 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)制形式訪問(wè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è)更完整的例子,說(shuō)明如何使用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 - 獲取資源大小:我無(wú)法正確讀取 _binary_foo_bar_size.在運(yùn)行時(shí),gdb 通過(guò)使用 display (unsigned int)&_binary_foo_bar_size 向我顯示文本資源的正確大小.但是將其分配給變量總是給出錯(cuò)誤的值.我可以通過(guò)以下方式解決這個(gè)問(wèn)題:


    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)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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?(如何將枚舉類(lèi)型變量轉(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)嗎?)
    主站蜘蛛池模板: 专业深孔加工_东莞深孔钻加工_东莞深孔钻_东莞深孔加工_模具深孔钻加工厂-东莞市超耀实业有限公司 | 回收二手冲床_金丰旧冲床回收_协易冲床回收 - 大鑫机械设备 | 棕刚玉-白刚玉厂家价格_巩义市东翔净水材料厂 | 重庆中专|职高|技校招生-重庆中专招生网 | 冷油器-冷油器换管改造-连云港灵动列管式冷油器生产厂家 | 东风体检车厂家_公共卫生体检车_医院体检车_移动体检车-锦沅科贸 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 硫酸钡厂家_高光沉淀硫酸钡价格-河南钡丰化工有限公司 | 德州网站制作 - 网站建设设计 - seo排名优化 -「两山建站」 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | ISO9001认证咨询_iso9001企业认证代理机构_14001|18001|16949|50430认证-艾世欧认证网 | 骁龙云呼电销防封号系统-axb电销平台-外呼稳定『免费试用』 | 台湾HIWIN上银直线模组|导轨滑块|TBI滚珠丝杆丝杠-深圳汉工 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 郑州巴特熔体泵有限公司专业的熔体泵,熔体齿轮泵与换网器生产厂家 | 软文发布平台 - 云软媒网络软文直编发布营销推广平台 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | app开发|app开发公司|小程序开发|物联网开发||北京网站制作|--前潮网络 | 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | 自动气象站_农业气象站_超声波气象站_防爆气象站-山东万象环境科技有限公司 | 成都网站建设制作_高端网站设计公司「做网站送优化推广」 | 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 砂磨机_立式纳米砂磨机_实验室砂磨机-广州儒佳化工设备厂家 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | T恤衫定做,企业文化衫制作订做,广告T恤POLO衫定制厂家[源头工厂]-【汉诚T恤定制网】 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 河南膏药贴牌-膏药代加工-膏药oem厂家-洛阳今世康医药科技有限公司 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 精准猎取科技资讯,高效阅读科技新闻_科技猎 | 青岛侦探调查_青岛侦探事务所_青岛调查事务所_青岛婚外情取证-青岛狄仁杰国际侦探公司 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 |