xto*_*ofl 46 c c++ linker gcc embedded-resource
一些框架(Qt,Windows,Gtk ...)提供了向二进制文件添加资源的功能.我想知道是否有可能在没有框架的情况下实现这一点,因为所有真正需要的是
如何使用gcc工具链实现这一目标?
nos*_*nos 48
你可以这样做:
objcopy --input binary \
--output elf32-i386 \
--binary-architecture i386 my_file.xml myfile.o
Run Code Online (Sandbox Code Playgroud)
这会生成一个可以链接到可执行文件的目标文件.此文件将包含这些符号,您必须在C代码中声明这些符号才能使用它们
00000550 D _binary_my_file_xml_end
00000550 A _binary_my_file_xml_size
00000000 D _binary_my_file_xml_start
Run Code Online (Sandbox Code Playgroud)
Lig*_*ica 31
最基本的,等价物是一个char
充满字节的数组.
在Linux上,您可以使用xxd -i <file>
"将"文件"编译"到char
数组中,然后将数组链接到二进制文件中,然后根据需要使用组成字节.
这是我自己的代码中的一个示例makefile
,它创建了一个"资源文件",templates.h
其中包含一堆char
表示HTML模板的数组:
templates.h:
@echo "#ifndef REDACTED_TEMPLATES_H" > templates.h
@echo "#define REDACTED_TEMPLATES_H" >> templates.h
@echo "// Auto-generated file! Do not modify!" >> templates.h
@echo "// NB: arrays are not null-terminated" >> templates.h
@echo "// (anonymous namespace used to force internal linkage)" >> templates.h
@echo "namespace {" >> templates.h
@echo "namespace templates {" >> templates.h
@cd templates;\
for i in * ;\
do \
echo "Compiling $$i...";\
xxd -i $$i | sed -e 's/ =/ __attribute__((unused)) =/' >> ../templates.h;\
done;\
cd ..
@echo "}" >> templates.h
@echo "}" >> templates.h
@echo "#endif" >> templates.h
Run Code Online (Sandbox Code Playgroud)
(另请参阅: 如何以编程方式将`__attribute __((unused))`应用于这些自动生成的对象?)
结果看起来有点像:
#ifndef REDACTED_TEMPLATES_H
#define REDACTED_TEMPLATES_H
// Auto-generated file! Do not modify!
// NB: arrays are not null-terminated
// (anonymous namespace used to force internal linkage)
namespace {
namespace templates {
unsigned char alert_email_finished_events_html[] __attribute__((unused)) = {
0x3c, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73,
0x3d, 0x22, 0x6e, 0x6f, 0x64, 0x65, 0x2d, 0x69, 0x6e, 0x66, 0x6f, 0x2d,
[..]
0x7d, 0x7d, 0x0d, 0x0a, 0x3c, 0x2f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e,
0x0d, 0x0a
};
unsigned int alert_email_finished_events_html_len __attribute__((unused)) = 290;
unsigned char alert_email_finished_events_list_html[] __attribute__((unused)) = {
0x3c, 0x74, 0x72, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x73,
0x65, 0x70, 0x61, 0x72, 0x61, 0x74, 0x65, 0x2d, 0x70, 0x72, 0x65, 0x76,
[..]
0x73, 0x74, 0x7d, 0x7d, 0x0d, 0x0a
};
unsigned int alert_email_finished_events_list_html_len __attribute__((unused)) = 42;
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
请注意,在仅在一个翻译单元中使用资源时,此特定示例是最佳的,但可以调整一般方法以满足您的需要.
归档时间: |
|
查看次数: |
1692 次 |
最近记录: |