我可以将普通文件链接到我的可执行文件中吗?
Can I link a plain file into my executable?
某些框架(Qt,Windows,Gtk...)提供了向二进制文件添加资源的功能。 我想知道是否有可能在没有框架的情况下实现这一目标,因为真正需要
的是- 在二进制文件(数据段)中包含资源地址的符号
- 表示资源长度的符号
- 资源本身
如何使用 gcc 工具链实现这一点?
你可以
这样做:
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
最基本的等价物是一个充满字节的char
数组。
在 Linux 上,您可以使用 xxd -i <file>
将文件"编译"为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
(另请参阅:如何以编程方式最好地将"__attribute__(未使用))"应用于这些自动生成的对象?
结果看起来有点像:
#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
请注意,当仅在一个翻译单元中使用资源时,此特定示例是最佳选择,但可以调整常规方法以满足您的需求。
相关文章:
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 共享库 (.so) 没有扩展名的 Linux 可执行文件之间的区别?
- 谷歌测试可执行文件中的单元测试未被vstest选中
- 如何在 c++ 中打开可执行文件附近的文件夹中的图像
- 调试Visual Studio中可执行文件调用的C 文件
- 我可以定义一个(键入的)常数,该常数确定不占据可执行文件中的空间
- cusparse功能的多个定义错误在链接可执行文件中的cuda文件时
- 对已编译的可执行文件中的常量字符串(例如密码)进行加密
- 使用 php 将带有命令行可执行文件路径的参数传递
- 将文件的内容重定向为可执行文件linux的标准输入
- 将配置文件存储在C 中的可执行文件中的方法
- Unix可执行文件上的Mac分段错误,文件编译正常
- 正在加载与可执行文件合并的文件
- 如何使用c++执行附加在可执行文件末尾的机器代码
- 在C++(Windows)中修改可执行文件中的十六进制值
- 如何在C++中调用预编译可执行文件中的函数(通过命令调用)
- 将Node.js+Socket.io封装到OSX可执行文件中的可靠方法?(或C/C++/Objective-C库作为替代
- C++数据类型及其对可执行文件大小的影响
- 是否可以不让我的可执行文件中的符号可用于动态打开的库
- 如何为c++可执行文件中的main()方法使用main.cpp以外的文件名