问题描述
我试图二进制的blob嵌入到一个exe文件。我使用MinGW的海湾合作委员会。
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;
}
然后我编译如下:
Then I compile like this:
gcc -o test.exe test.c binary.o
不过,我总是得到:
But I always get:
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编译器常(总是?)似乎prePEND一个下划线的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编译器,以prePEND领先强调所有外部范围的程序标识符,以避免与运行时语言支持贡献冲突常见的做法
但是这让我说,如果下划线是prepended所有实习医生,那么你就没有真正分割空间非常多。无论如何,这是另一天的问题,而事实是,下划线也得到补充。
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的二进制斑点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!