如何使用 autotools 在 64 位平台上创建 32 位共享库
我正在使用自动工具来构建我的系统,该系统主要由一个库组成。在 64 位 Red Hat 平台上,我需要能够生成一个能够在 32 位 Red Hat 平台上运行的库。
当我将 -m32
添加到编译行时,一切正常,可以生成静态 (.a
) 库,但是一旦我尝试创建共享库,我就会发现得到这样的错误:
/usr/bin/ld: warning: i386:x86-64 architecture of input file `/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o' is incompatible with i386 output
/usr/bin/ld: warning: i386:x86-64 architecture of input file `/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbeginS.o' is incompatible with i386 output
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbeginS.o: In function `__do_global_dtors_aux':
crtstuff.c:(.text+0x29): undefined reference to `__DTOR_END__'
collect2: ld returned 1 exit status'
我可以看到问题是它包含 /usr/lib64 中的 64 位目标文件,而不是 /usr/lib 中的正确 32 位目标文件(它们就在那里),但我可以不知道如何解决它。
I'm using autotools to build my system, which consists primarily of a library. On 64-bit Red Hat platforms, I need to be able to produce a library capable of working on 32-bit Red Hat platforms.
When I add -m32
to the compile lines everything works fine to produce a static (.a
) library, but as soon as I try to create a shared-library, I get error like this:
/usr/bin/ld: warning: i386:x86-64 architecture of input file `/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o' is incompatible with i386 output
/usr/bin/ld: warning: i386:x86-64 architecture of input file `/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbeginS.o' is incompatible with i386 output
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbeginS.o: In function `__do_global_dtors_aux':
crtstuff.c:(.text+0x29): undefined reference to `__DTOR_END__'
collect2: ld returned 1 exit status'
I can see the problem is that it's including 64-bit object files out of /usr/lib64 instead of the correct 32-bit ones out of /usr/lib (they're there alright), but I can't figure out how to fix it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,确保您有对 32 位编译的编译器/libc 支持。在像 Ubuntu 这样的一些发行版中,您需要做的是安装软件包
gcc-multilib
和/或g++-multilib
:然后,在调用 configure 时,指定一个 32 位主机并传递 32 位编译标志:
如果您没有安装 multilib,则在传递
-m32
时,您将收到类似configure: error: C compiler cannot createexecutables
的错误旗帜。First, make sure you have compiler/libc support for 32-bit compilation. In some distros like Ubuntu, what you need to do is install packages
gcc-multilib
and/org++-multilib
:Then, when calling configure, specify a 32-bit host and pass 32-bit compilation flags:
If you do not have multilib installed, you will get an error like
configure: error: C compiler cannot create executables
when passing the-m32
flag.我在 RHEL6 上遇到了这个问题。这个有效的
LTCC=gcc -m32
是让 libtool 构建 32 位库所需的魔法咒语I had this problem on RHEL6. This worked
LTCC=gcc -m32
was the magic incantation needed to get libtool to build library 32 bit我也遇到过同样的问题。但我不使用自动工具。然后,在手动编辑的 Makefile 中,我注意到行中
没有 gcc 选项来指示 32 位架构。一旦我的 CFLAGS 已经有了选项 -m32,我决定将其放在上面提到的行中:
瞧。有用!
因此,在自动工具中,也许设置 CFLAGS 变量以包含 -m32 选项也适合您。
希望我有所帮助...
I've had this same problem. But I don't use autotools. Then, in the Makefile edited by hand, I noticed that in line
there was no option to gcc that indicates the 32bit architeture. Once my CFLAGS has already the option -m32, I decided to put it in the line mentioned above:
and voilà. It works!
So, in autotools, maybe setting CFLAGS variable to include -m32 option works for you too.
Hope I have helped...
您会尝试:
在您的 makefile 中,因为某些脚本尝试使用 gcc 或 g++ 而不是我们期望的 ld 进行链接?
更新:如果您手动修改每个 gcc/g++ 调用,只需尝试使用 -m32 -Wl,-m32 而不是简单的 -m32 作为附加选项。
Would you try:
in your makefile, since some scripts try linking using gcc or g++ instead of ld as we expect?
Update: In case you manually modifying every gcc/g++ call, just try use -m32 -Wl,-m32 instead of simple -m32 as additional option.
我也遇到过同样的问题:在 Ubuntu 64 位机器上运行时,我设法使用
export CFLAGS=-m32; 编译和链接 32 位主机。 ./configure --host=i386
,但 libtool 仍会生成 64 位共享库。我通过创建一个 32 位构建环境并对其进行 chroot 来解决这个问题。 Ubuntu 通过 debootstrap 使这一切变得简单。
I've had this same problem: Running on an Ubuntu 64-bit machine, I managed to compile and link for 32-bit hosts using
export CFLAGS=-m32; ./configure --host=i386
, but libtool would still generate a 64-bit shared library.I worked around this by creating a 32-bit build environment and chrooting into it. Ubuntu makes this easy via debootstrap.
在 64 位机器上构建 32 位共享对象的 GNU 链接器标志是:
-m elf_i386
因此,请在 Makefile 中写入:
The GNU linker flag to build a 32 bit shared object on a 64 bit machine is:
-m elf_i386
So please e.g. write in the Makefile:
快速修复...
在另一台具有 32 位 Linux 的 PC 上构建应用程序并传输结果
应用程序文件(库等)到所需的 64 位 Linux 机器。看看是否有效。这对我有用。
A quick fix....
Build application on a different PC with 32 bit linux and transfer the resulted
application files ( library, etc) to the desired 64 bit linux machine. See if it works. It worked for me.