How to compile 32bit x86 application in 64bit x86 environment?
Any command for cc/ld/ar, including options? Thanks.
Any links is well appreciated. Thanks.
Note: take c code for example.
To compile and link a C source file with a 64-bit multilib GCC, you can do the following:
gcc -m32 -c somefile.c
gcc -m32 somefile.o -o myprog
Note that all 32-bit libraries need to be installed and useable by the multilib compiler.
ar
should work, if built correctly, it is discouraged to call ld
directly, because its options are radically different from GCC's. Just link with GCC.
As to why it is "discouraged to call ld
directly": If you all gcc
to link, it will know exactly where system/runtime libraries are located, and also about any platform-specific options it needs to pass to ld
. When calling ld
directly, you need to take care of all of that. Here that matters for the options for 32 vs 64-bit, along with proper library directories.
是的,只需使用-m32
并确保已安装所有32位工具和库(默认情况下并非所有x86-64发行版都包含这些工具和库,因此您可能需要apt-get
或yast
或安装它们的任何方式)。
$ gcc -m32 -Wall foo.c -o foo
The -m32
flag is all you need, ie.
gcc -m32 ...
If you get an error, you may need the 32-bit libraries, that might be named similar to glibc-devel.i686
. That's the name of the package on Fedora (using yum
), other Linux distros should be similar.
On Debian & Ubuntu, you'll need the gcc-multilib
and ia32-libs-dev
packages.
Yes, I needed -D_FILE_OFFSET_BITS=64 sometimes -m32 makes some trouble sometimes so you have to try yourself.
c++ -m32 -D_FILE_OFFSET_BITS=64 foo.c -o foo
But that's for the other way round. Compiling 64bit programs on 32bit boxes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.