問題描述
我正在尋找一種方法,可以在 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模板網!