使用 gcc mingw 嵌入二进制 blob [英] Embedding binary blobs using gcc mingw
问题描述
我正在尝试将二进制 blob 嵌入到 exe 文件中.我正在使用 mingw gcc.
I am trying to embed binary blobs into an exe file. I am using mingw gcc.
我像这样制作目标文件:
I make the object file like this:
ld -r -b binary -o binary.o input.txt
然后我查看 objdump 输出以获取符号:
I then look objdump output to get the symbols:
objdump -x binary.o
它给出了名为:
_binary_input_txt_start
_binary_input_txt_end
_binary_input_txt_size
然后我尝试在我的 C 程序中访问它们:
I then try and access them in my C program:
#include <stdlib.h>
#include <stdio.h>
extern char _binary_input_txt_start[];
int main (int argc, char *argv[])
{
char *p;
p = _binary_input_txt_start;
return 0;
}
然后我这样编译:
gcc -o test.exe test.c binary.o
但我总是得到:
undefined reference to _binary_input_txt_start
有人知道我做错了什么吗?
Does anyone know what I am doing wrong?
推荐答案
在你的 C 程序中删除前导下划线:
In your C program remove the leading underscore:
#include <stdlib.h>
#include <stdio.h>
extern char binary_input_txt_start[];
int main (int argc, char *argv[])
{
char *p;
p = binary_input_txt_start;
return 0;
}
C 编译器经常(总是?)似乎在 extern
名称前面加上下划线.我不完全确定为什么会这样 - 我认为 这篇维基百科文章是有道理的 声称
C compilers often (always?) seem to prepend an underscore to extern
names. I'm not entirely sure why that is - I assume that there's some truth to this wikipedia article's claim that
C 编译器的常见做法是在所有外部作用域程序标识符前加上前导下划线,以避免与运行时语言支持的贡献发生冲突
It was common practice for C compilers to prepend a leading underscore to all external scope program identifiers to avert clashes with contributions from runtime language support
但让我吃惊的是,如果在所有 externs 前面加上下划线,那么你并没有真正对命名空间进行太多分区.无论如何,这是另一天的问题,事实是确实添加了下划线.
But it strikes me that if underscores were prepended to all externs, then you're not really partitioning the namespace very much. Anyway, that's a question for another day, and the fact is that the underscores do get added.
这篇关于使用 gcc mingw 嵌入二进制 blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!