使用 Clang 编译时,链接器无法在 64 位 Mint OS 下找到 32 位库

Linker fails to find 32-bit libraries under 64-bit Mint OS when compiling with Clang

我刚刚在 64 位 Mint OS(版本 17)上安装了 Clang 3.5。我正在尝试以 32 位编译我的程序,但是我 运行 遇到了问题。

我的主目录中有一个名为 test.cpp 的文件,其中包含以下代码:

#include <iostream>

int main()
{
    std::cout << "Hello" << std::endl;
}

然后我运行执行以下命令:

$ clang++ -m32 -Wl,--verbose test.cpp | grep libgcc

我得到以下输出:

$ clang++ -m32 -Wl,--verbose test.cpp | grep libgcc
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so succeeded
/usr/bin/ld: cannot find -lgcc_s
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.a failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../i386-linux-gnu/libgcc_s.so failed
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../i386-linux-gnu/libgcc_s.a failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/libgcc_s.so failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/libgcc_s.a failed
attempt to open /usr/bin/../lib/i386-linux-gnu/libgcc_s.so failed
/usr/bin/ld: cannot find -lgcc
attempt to open /usr/bin/../lib/i386-linux-gnu/libgcc_s.a failed
attempt to open /usr/bin/../lib32/libgcc_s.so failed
attempt to open /usr/bin/../lib32/libgcc_s.a failed
attempt to open /lib/i386-linux-gnu/libgcc_s.so failed
attempt to open /lib/i386-linux-gnu/libgcc_s.a failed
attempt to open /lib/../lib32/libgcc_s.so failed
attempt to open /lib/../lib32/libgcc_s.a failed
attempt to open /usr/lib/i386-linux-gnu/libgcc_s.so failed
attempt to open /usr/lib/i386-linux-gnu/libgcc_s.a failed
attempt to open /usr/lib/../lib32/libgcc_s.so failed
attempt to open /usr/lib/../lib32/libgcc_s.a failed
attempt to open /usr/lib/x86_64-linux-gnu/../../lib32/libgcc_s.so failed
attempt to open /usr/lib/x86_64-linux-gnu/../../lib32/libgcc_s.a failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../libgcc_s.so failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../libgcc_s.a failed
attempt to open /usr/bin/../lib/libgcc_s.so failed
attempt to open /usr/bin/../lib/libgcc_s.a failed
attempt to open /lib/libgcc_s.so failed
attempt to open /lib/libgcc_s.a failed
attempt to open /usr/lib/libgcc_s.so failed
attempt to open /usr/lib/libgcc_s.a failed
attempt to open /usr/i386-linux-gnu/lib32/libgcc_s.so failed
attempt to open /usr/i386-linux-gnu/lib32/libgcc_s.a failed
attempt to open /usr/x86_64-linux-gnu/lib32/libgcc_s.so failed
attempt to open /usr/x86_64-linux-gnu/lib32/libgcc_s.a failed
attempt to open //usr/local/lib/i386-linux-gnu/libgcc_s.so failed
attempt to open //usr/local/lib/i386-linux-gnu/libgcc_s.a failed
attempt to open //usr/local/lib32/libgcc_s.so failed
attempt to open //usr/local/lib32/libgcc_s.a failed
attempt to open //lib/i386-linux-gnu/libgcc_s.so failed
attempt to open //lib/i386-linux-gnu/libgcc_s.a failed
attempt to open //lib32/libgcc_s.so failed
attempt to open //lib32/libgcc_s.a failed
attempt to open //usr/lib/i386-linux-gnu/libgcc_s.so failed
attempt to open //usr/lib/i386-linux-gnu/libgcc_s.a failed
attempt to open //usr/lib32/libgcc_s.so failed
attempt to open //usr/lib32/libgcc_s.a failed
attempt to open //usr/local/lib/libgcc_s.so failed
attempt to open //usr/local/lib/libgcc_s.a failed
attempt to open //lib/libgcc_s.so failed
attempt to open //lib/libgcc_s.a failed
attempt to open //usr/lib/libgcc_s.so failed
attempt to open //usr/lib/libgcc_s.a failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc.so failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc.a succeeded
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../i386-linux-gnu/libgcc.so failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../i386-linux-gnu/libgcc.a failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/libgcc.so failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/libgcc.a failed
attempt to open /usr/bin/../lib/i386-linux-gnu/libgcc.so failed
attempt to open /usr/bin/../lib/i386-linux-gnu/libgcc.a failed
attempt to open /usr/bin/../lib32/libgcc.so failed
attempt to open /usr/bin/../lib32/libgcc.a failed
attempt to open /lib/i386-linux-gnu/libgcc.so failed
attempt to open /lib/i386-linux-gnu/libgcc.a failed
attempt to open /lib/../lib32/libgcc.so failed
attempt to open /lib/../lib32/libgcc.a failed
attempt to open /usr/lib/i386-linux-gnu/libgcc.so failed
attempt to open /usr/lib/i386-linux-gnu/libgcc.a failed
attempt to open /usr/lib/../lib32/libgcc.so failed
attempt to open /usr/lib/../lib32/libgcc.a failed
attempt to open /usr/lib/x86_64-linux-gnu/../../lib32/libgcc.so failed
attempt to open /usr/lib/x86_64-linux-gnu/../../lib32/libgcc.a failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../libgcc.so failed
attempt to open /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../libgcc.a failed
attempt to open /usr/bin/../lib/libgcc.so failed
attempt to open /usr/bin/../lib/libgcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/libgcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/libgcc.a failed
attempt to open /usr/i386-linux-gnu/lib32/libgcc.so failed
attempt to open /usr/i386-linux-gnu/lib32/libgcc.a failed
attempt to open /usr/x86_64-linux-gnu/lib32/libgcc.so failed
attempt to open /usr/x86_64-linux-gnu/lib32/libgcc.a failed
attempt to open //usr/local/lib/i386-linux-gnu/libgcc.so failed
attempt to open //usr/local/lib/i386-linux-gnu/libgcc.a failed
attempt to open //usr/local/lib32/libgcc.so failed
attempt to open //usr/local/lib32/libgcc.a failed
attempt to open //lib/i386-linux-gnu/libgcc.so failed
attempt to open //lib/i386-linux-gnu/libgcc.a failed
attempt to open //lib32/libgcc.so failed
attempt to open //lib32/libgcc.a failed
attempt to open //usr/lib/i386-linux-gnu/libgcc.so failed
attempt to open //usr/lib/i386-linux-gnu/libgcc.a failed
attempt to open //usr/lib32/libgcc.so failed
attempt to open //usr/lib32/libgcc.a failed
attempt to open //usr/local/lib/libgcc.so failed
attempt to open //usr/local/lib/libgcc.a failed
attempt to open //lib/libgcc.so failed
attempt to open //lib/libgcc.a failed
attempt to open //usr/lib/libgcc.so failed
attempt to open //usr/lib/libgcc.a failed
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我已经尽我所能完成了所有谷歌搜索和自我调查,我不确定为什么这个简单的例子不起作用。谁能帮我弄清楚我遗漏了什么?

您需要安装一个 multilib g++ sudo apt-get install g++-multilib

g++-multilib 还应该安装所有其他必需的依赖项,例如 libc6-i386.

以下是使用 clang 进行交叉编译时需要确保的事项:

  • 您是否安装了 32 位 (x86) C++ 库? (看起来你有,但只是确定一下。)

  • 尝试使用 --sysroot=<location of your 32-bit multilibs>。这将告诉 clang 在您告诉它的特定目录中搜​​索 binlibinclude 目录,而不是使用其(定义不明确的)硬编码路径。

  • 尝试使用 -I-L 选项来更具体地告诉 clang 你的 include 和 lib 目录在哪里。

  • 最坏情况如果 none 以上工作:尝试使用 -B <path> 选项告诉 clang 具体使用哪个链接器。也尝试将此与前面的选项结合使用。

请参阅 http://clang.llvm.org/docs/CrossCompilation.html 了解更多详情(尽管此处未涵盖 -B 选项)

在 64 位机器上(例如 AMD64),您必须安装 32 位库 et.al。例如,在 Debian 10 (Buster) 上,这是:

sudo apt 安装 libc6-i386 lib32gcc1 lib32gcc-8-dev gcc-8-multilib

在 Debian 11 (Bullseye) 上,这是:

sudo apt 安装 libc6-i386 lib32gcc-10-dev gcc-10-multilib

(对于 C++,分别添加 g++-8-multilib 或 g++-10-multilib)

完成这些安装后,您将在 /lib32 和 /usr/lib32 中获得新的 32 位库。此外,在 /etc/ld.so.conf.d 中你会发现新配置(/sbin/ldconfig 由 apt 安装自动执行)。

在 Debian Buster 上,我可以使用 gcc 和 -m32 开关轻松编译 (gcc-8.3.0)。在 Debian Bullseye (gcc-10.2.0) 上,单独使用 -m32 的 gcc 让我很难受。

32 位可执行文件的 gcc 正确执行是在 Bullseye 上:

gcc -m32 -Wl,--rpath-link,/usr/lib32 -L/usr/lib32 -nodefaultlibs -nostdlib -o /usr/lib32/crti.o /usr/lib32/crt1.o /usr/lib32/crtn.o -lc -lgcc_s <一个或多个 C 文件>