如何使用 autotools 在 64 位平台上创建 32 位共享库

发布于 2024-10-24 20:01:54 字数 807 浏览 10 评论 0原文

我正在使用自动工具来构建我的系统,该系统主要由一个库组成。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最笨的告白 2024-10-31 20:01:54

首先,确保您有对 32 位编译的编译器/libc 支持。在像 Ubuntu 这样的一些发行版中,您需要做的是安装软件包 gcc-multilib 和/或 g++-multilib

sudo apt-get install gcc-multilib g++-multilib

然后,在调用 configure 时,指定一个 32 位主机并传递 32 位编译标志:

./configure --host=i686-linux-gnu "CFLAGS=-m32" "CXXFLAGS=-m32" "LDFLAGS=-m32"

如果您没有安装 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/or g++-multilib:

sudo apt-get install gcc-multilib g++-multilib

Then, when calling configure, specify a 32-bit host and pass 32-bit compilation flags:

./configure --host=i686-linux-gnu "CFLAGS=-m32" "CXXFLAGS=-m32" "LDFLAGS=-m32"

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.

じее 2024-10-31 20:01:54

我在 RHEL6 上遇到了这个问题。这个有效的

./configure --host=i386-redhat-linux --build=i386-redhat-linux "CFLAGS=-m32" "CXXFLAGS=-m32" "LDFLAGS=-m32" "LTCC=gcc -m32"

LTCC=gcc -m32 是让 libtool 构建 32 位库所需的魔法咒语

I had this problem on RHEL6. This worked

./configure --host=i386-redhat-linux --build=i386-redhat-linux "CFLAGS=-m32" "CXXFLAGS=-m32" "LDFLAGS=-m32" "LTCC=gcc -m32"

LTCC=gcc -m32 was the magic incantation needed to get libtool to build library 32 bit

凹づ凸ル 2024-10-31 20:01:54

我也遇到过同样的问题。但我不使用自动工具。然后,在手动编辑的 Makefile 中,我注意到行中

$(CC) -shared -Wl,-soname,lib$(NAME).so.0 -o lib$(NAME).so.$(VERSION) $(OBJ)

没有 gcc 选项来指示 32 位架构。一旦我的 CFLAGS 已经有了选项 -m32,我决定将其放在上面提到的行中:

$(CC) $(CFLAGS) -shared -Wl,-soname,lib$(NAME).so.0 -o lib$(NAME).so.$(VERSION) $(OBJ)

瞧。有用!

因此,在自动工具中,也许设置 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

$(CC) -shared -Wl,-soname,lib$(NAME).so.0 -o lib$(NAME).so.$(VERSION) $(OBJ)

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:

$(CC) $(CFLAGS) -shared -Wl,-soname,lib$(NAME).so.0 -o lib$(NAME).so.$(VERSION) $(OBJ)

and voilà. It works!

So, in autotools, maybe setting CFLAGS variable to include -m32 option works for you too.

Hope I have helped...

以为你会在 2024-10-31 20:01:54

您会尝试:

CFLAGS=-m32 -Wl,-m32
CXXFLAGS=-m32 -Wl,-m32
LDFLAGS=-m32

在您的 makefile 中,因为某些脚本尝试使用 gcc 或 g++ 而不是我们期望的 ld 进行链接?

更新:如果您手动修改每个 gcc/g++ 调用,只需尝试使用 -m32 -Wl,-m32 而不是简单的 -m32 作为附加选项。

Would you try:

CFLAGS=-m32 -Wl,-m32
CXXFLAGS=-m32 -Wl,-m32
LDFLAGS=-m32

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.

半山落雨半山空 2024-10-31 20:01:54

我也遇到过同样的问题:在 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.

不爱素颜 2024-10-31 20:01:54

在 64 位机器上构建 32 位共享对象的 GNU 链接器标志是:-m elf_i386
因此,请在 Makefile 中写入:

LDFLAGS=-m elf_i386

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:

LDFLAGS=-m elf_i386
故事与诗 2024-10-31 20:01:54

快速修复...

在另一台具有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文