在许多嵌入式开发的系统中,由于没有文件系统,所以对于文件的处理不太方便。如果是一个很小的文件,可以做成数组形式。可如果文件稍大该如何处理呢?有没有一种更好的办法?呵呵,办法当然是有的。
假定我们有一个文本文件(一般需要嵌入的都是图片文件或者bin文件,此处只是为了方便演示)data,需要把这个文件做成.o文件,并编译到最终程序中。
首先执行objcopy命令:objcopy -I binary -O pe-i386 -B i386 data data.o 由于本人是在Cygwin下测试,所以-O选择执行程序文件格式为pe-i386,-B选择架构为i386。
然后用nm命令查看生成的data.o文件:
$ nm data.o
00000241 D _binary_data_end
00000241 A _binary_data_size
00000000 D _binary_data_start
好了,我们看到data.o中有三个对象,_binary_data_end、_binary_data_size和_binary_data_start,对象生成的命名规则是固定的,所以不用nm命令也可以推测出这三个对象的名字。
好了,现在需要在代码中引用它们了,需要先声明:
extern char binary_data_start;
extern int binary_data_size;
extern char binary_data_end;
完整的C代码如下:
编译cc data.o test.c -o test
执行结果:
$ ./test You Are Beautiful My life is brilliant My life is brilliant My love is pure I saw an angel Of that I'm sure She smiled at me on the subway She was with another man But I won't lose no sleep on that night 'Cause I've got a plan You're beautiful, you're beautiful You're beautiful, it's ture I saw your face in a crowded place And I don't know what to do 'Cause I'll never be with you Yes, she caught my eye As I walked on by She could see from my face that I was freaking high And I don't think that I'll see her again But we shared a moment that will last util the end ... String length: 577需要注意的是Cygwin和Linux对于符号表的引用是不同的,Linux下nm导出的符号表和C代码中引用的变量名是一致的,Cygwin代码中引用需要去掉“_”。另外_binary_data_size这个变量比较特殊,对它取地址可以得到文件的长度。至于它本身定义成什么类型,似乎无关紧要。本人对此有个知识盲点,即只知道这样使用是可以,但不知道该变量本身的具体意义。查看nm的man命令,对于A类型的解释是“The symbol's value is absolute, and will not be changed by further linking.”,就是说这是一个绝对值,在将来链接时也不会改变。哪位高人看到此文,希望不吝指教。