Linux 强制 gcc 在 64 位平台上编译 32 位程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3501878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:21:46  来源:igfitidea点击:

Force gcc to compile 32 bit programs on 64 bit platform

linuxgcc32-bitx86-64

提问by Jure1873

I've got a proprietary program that I'm trying to use on a 64 bit system.

我有一个我试图在 64 位系统上使用的专有程序。

When I launch the setup it works ok, but after it tries to update itself and compile some modules and it fails to load them.

当我启动设置时,它工作正常,但在它尝试自我更新并编译一些模块后,它无法加载它们。

I'm suspecting it's because it's using gcc and gcc tries to compile them for a 64 bit system and therefore this program cannot use these modules.

我怀疑这是因为它使用 gcc 并且 gcc 尝试为 64 位系统编译它们,因此该程序无法使用这些模块。

Is there any way (some environmental variables or something like that) to force gcc to do everything for a 32 bit platform. Would a 32 bit chroot work?

有没有办法(一些环境变量或类似的东西)强制 gcc 为 32 位平台做所有事情。32 位 chroot 可以工作吗?

采纳答案by Alan Pearce

You need to make GCC use the -m32flag.

您需要让 GCC 使用该-m32标志。

You could try writing a simple shell script to your $PATHand call it gcc (make sure you don't overwrite the original gcc, and make sure the new script comes earlier in $PATH, and that it uses the full path to GCC.

您可以尝试为您编写一个简单的 shell 脚本$PATH并将其命名为 gcc(确保您没有覆盖原始 gcc,并确保新脚本更早出现在$PATH.

I think the code you need is just something like /bin/gcc -m32 $*depending on your shell (the $*is there to include all arguments, although it might be something else – very important!)

我认为您需要的代码就像/bin/gcc -m32 $*取决于您的 shell($*包含所有参数,尽管它可能是其他东西 - 非常重要!)

回答by Geoffrey R.

You may get a 32-bit binary by applying Alan Pearce's method, but you may also get errors as follows:

您可能会通过应用 Alan Pearce 的方法获得 32 位二进制文​​件,但您也可能会遇到如下错误:

fatal error: bits/predefs.h: No such file or directory

If this is the case and if you have apt-get, just install gcc-multilib

如果是这种情况并且您有 apt-get,只需安装 gcc-multilib

sudo apt-get install gcc-multilib 

回答by e271p314

For any code that you compile directly using gcc/g++, you will need to add -m32option to the compilation command line, simply edit your CFLAGS, CXXFLAGSand LDFLAGSvariables in your Makefile.

对于您直接编译使用任何代码gcc/ g++,你将需要添加-m32选项编译命令行,只需编辑CFLAGSCXXFLAGSLDFLAGS在您的变量Makefile

For any 3rd party code you might be using you must make sure when you build it to configure it for cross compilation. Run ./configure --helpand see which option are available. In most cases you can provide your CFLAGS, CXXFLAGSand LDFLAGSvariables to the configure script. You also might need to add --buildand --hostto the configure script so you end up with something like

对于您可能正在使用的任何 3rd 方代码,您必须确保在构建它时配置它以进行交叉编译。运行./configure --help并查看可用的选项。在大多数情况下,您可以将CFLAGS,CXXFLAGSLDFLAGS变量提供给配置脚本。您可能还需要将--build和添加--host到配置脚本中,以便最终得到类似

./configure CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32 --build=x86_64-pc-linux-gnu --host=i686-pc-linux-gnu

If compilation fails this probably means that you need to install some 32 bit development packages on your 64 bit machine

如果编译失败,这可能意味着您需要在 64 位机器上安装一些 32 位开发包