“重定位被截断以适合:R_X86_64_PC32 against `.bss'”当使用一个巨大的数组

问题描述 投票:0回答:0

以下 C++ 代码是我的真实代码的 MRE,完全删除了所有无助于触发错误的内容:

#include <cstring>

class X {
   private:
    char v[4L * 1024L * 1024L * 1024L];

   public:
    X() {
        memset(v, 0, sizeof(v));
    }
};

int main() {
    static X x;

    return 0;
}

这是我用 Clang 14 编译时得到的结果:

/tmp/x-6d6700.o: in function `main':
x.cpp:(.text+0x11): relocation truncated to fit: R_X86_64_PC32 against `.bss'
x.cpp:(.text+0x1f): relocation truncated to fit: R_X86_64_PC32 against `.bss'
x.cpp:(.text+0x45): relocation truncated to fit: R_X86_64_PC32 against `.bss'
x.cpp:(.text+0x65): relocation truncated to fit: R_X86_64_PC32 against `.bss'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

将数组的大小减少到小于 4 GB 的 RAM 修复它。

虽然我已经通过使用动态分配在我的真实代码中解决了这个问题,但我仍然很好奇是什么原因造成的(我想这是由于某些关键指令缺少 64 位立即编码)以及是否有可能修复它,也许通过一些链接器标志,而不是诉诸动态分配。

c++ linker-errors large-data
© www.soinside.com 2019 - 2024. All rights reserved.