<p>正如您所注意到的,编译器在这种大数据数组上崩溃。在</p>
<p>除了读取二进制文件之外(因为您不想这样做),您可以与程序集文件链接。它仍然使可执行文件自给自足,而且GAS对大文件的容忍度要高得多。下面是我使用python生成的一些asm文件的示例,它可以很好地与经典的<code>gcc</code>进行组装:</p>
<pre><code>.section .rodata
.globl FT
.globl FT_end
FT:
.byte 0x46,0x54,0x5f,0x43,0x46,0x0,0x0,0x0,0x0,0x0,0x0,0x3,0x43,0x4f,0x4d,0x50
.byte 0x32,0x30,0x31,0x0,0x3,0x88,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
.byte 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
.byte 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x28,0xe6,0x47,0x6,0x7,0x8,0x28,0x28
.byte 0x26,0x6,0x2a,0x6,0x6,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
FT_end:
</code></pre>
<p>这种技术允许我在可执行文件中嵌入一个80兆字节的二进制文件(100万个代码行),因为在那个环境中我没有文件系统来读取数据文件(QEMU)</p>
<p>用python代码进行了真正的测试,我终于发现:</p>
<p>Python代码:</p>
^{pr2}$
<p> ^ {CD2>}:C++代码(C++ 11,我为指针引用ASM部分而努力):</p>
<pre><code>#include <iostream>
#include <vector>
#include <strings.h>
extern const float extref;
extern const float extref_end;
int main()
{
int size = (&extref_end - &extref);
std::cout << "nb_elements: " << size << std::endl;
std::vector<float> v(size);
memcpy(&v[0],&extref,sizeof(float)*size);
for (auto it : v)
{
std::cout << it << std::endl;
}
return 0;
}
</code></pre>
<p>Python代码生成一个<code>data.s</code>文件。使用以下内容创建可执行文件:</p>
<pre><code>g++ -std=c++11 test.cpp data.s
</code></pre>
<p>运行:</p>
<pre><code>nb_elements: 5
0.12
0.45
0.34
4.567
22.7
</code></pre>
<p>这种方法的主要优点是,您可以用所需的格式定义任意多个符号。在</p>