【问题标题】:Can I link a plain file into my executable? [duplicate]我可以将纯文件链接到我的可执行文件中吗? [复制]
【发布时间】:2012-12-01 04:05:51
【问题描述】:

一些框架(Qt、Windows、Gtk...)提供向二进制文件添加资源的功能。我想知道是否有可能在没有框架的情况下实现这个,因为真正需要的是

  1. 在二进制(数据段)中包含资源地址的符号
  2. 表示资源长度的符号
  3. 资源本身

如何使用 gcc 工具链来实现这一点?

【问题讨论】:

  • 嗯,你可以实现所有这些东西,然后你将拥有一个资源框架。
  • @LightnessRacesinOrbit: 确实,但是没有 Gui、序列化、XML……我目前不需要:)
  • 是的,您将拥有一个资源框架,而不是庞然大物的万能框架。
  • @nos: ... 这个问题包含了我的问题的答案! stackoverflow.com/a/4865249/6610

标签: c++ c gcc linker embedded-resource


【解决方案1】:

你可以这样做:

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是否也存在于windows上吗?
  • @xtofl 如果您至少使用 mingw 或 cygwin,它可以在 Windows 上使用,但是将文件转换为 C 数组将是跨工具链最便携的事情。
【解决方案2】:

最基本的等价物是一个充满字节的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

另见:How best can I programmatically apply `__attribute__ ((unused))` to these auto-generated objects?

结果看起来有点像:

#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

请注意,当仅在一个翻译单元中使用资源时,此特定示例是最佳选择,但可以调整通用方法以满足您的需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多