C++ 我可以将普通文件链接到我的可执行文件中吗?
一些框架(Qt、Windows、Gtk…)提供了向二进制文件添加资源的功能。我想知道,如果没有该框架,是否有可能实现这一目标,因为真正需要的是C++ 我可以将普通文件链接到我的可执行文件中吗?,c++,c,gcc,linker,embedded-resource,C++,C,Gcc,Linker,Embedded Resource,一些框架(Qt、Windows、Gtk…)提供了向二进制文件添加资源的功能。我想知道,如果没有该框架,是否有可能实现这一目标,因为真正需要的是 在二进制(数据段)中包含资源地址的符号 表示资源长度的符号 资源本身 如何使用gcc工具链实现这一点?在最基本的方面,等价物是一个充满字节的char数组 在Linux上,您可以使用xxd-i将文件“编译”到char数组中,然后将数组链接到二进制文件中,并随意使用组成字节 下面是我自己代码的makefile中的一个示例,它创建了一个名为templates.
如何使用gcc工具链实现这一点?在最基本的方面,等价物是一个充满字节的
char
数组
在Linux上,您可以使用xxd-i
将文件“编译”到char
数组中,然后将数组链接到二进制文件中,并随意使用组成字节
下面是我自己代码的makefile
中的一个示例,它创建了一个名为templates.h
的“资源文件”,其中包含一组表示HTML模板的char
数组:
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
(另请参见:)
结果看起来有点像:
#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
请注意,当仅在一个翻译单元中使用资源时,此特定示例是最佳的,但一般的方法可以根据您的需要进行调整。最基本的等效方法是一个充满字节的
char
数组
在Linux上,您可以使用xxd-i
将文件“编译”到char
数组中,然后将数组链接到二进制文件中,并随意使用组成字节
下面是我自己代码的makefile
中的一个示例,它创建了一个名为templates.h
的“资源文件”,其中包含一组表示HTML模板的char
数组:
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
(另请参见:)
结果看起来有点像:
#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
请注意,当仅在一个翻译单元中使用资源时,此特定示例是最佳的,但一般方法可以根据您的需要进行调整。您可以这样做:
objcopy --input binary \
--output elf32-i386 \
--binary-architecture i386 my_file.xml myfile.o
这将生成一个可以链接到可执行文件的对象文件。
此文件将包含这些符号,您必须在C代码中声明这些符号才能使用
能够使用它们
00000550 D _binary_my_file_xml_end
00000550 A _binary_my_file_xml_size
00000000 D _binary_my_file_xml_start
您可以这样做:
objcopy --input binary \
--output elf32-i386 \
--binary-architecture i386 my_file.xml myfile.o
这将生成一个可以链接到可执行文件的对象文件。
此文件将包含这些符号,您必须在C代码中声明这些符号才能使用
能够使用它们
00000550 D _binary_my_file_xml_end
00000550 A _binary_my_file_xml_size
00000000 D _binary_my_file_xml_start
好吧,你可以实现所有这些东西,然后你将拥有一个资源框架。@LightnessRacesinOrbit:的确,但是没有Gui、序列化、XML。。。我现在不需要:)是的,你将拥有一个资源框架,而不是一个庞然大物无所不能的框架。另请参见@nos:。。。这个问题包含了我问题的答案!好吧,你可以实现所有这些东西,然后你将拥有一个资源框架。@LightnessRacesinOrbit:的确,但是没有Gui、序列化、XML。。。我现在不需要:)是的,你将拥有一个资源框架,而不是一个庞然大物无所不能的框架。另请参见@nos:。。。这个问题包含了我问题的答案!正是我需要的。你知道windows上是否也有
objcopy
吗?@xtofl如果你至少使用mingw或cygwin,它在windows上是可用的,但是将文件转换成C数组将是跨工具链最可移植的事情。这正是我需要的。您是否知道windows上是否也存在objcopy
?@xtofl如果您至少使用mingw或cygwin,它在windows上是可用的,但是将文件转换为C数组将是跨工具链最可移植的事情。