Compile the 32-bit program under 64-bit Linux __linux

Source: Internet
Author: User
In general, we'd better compile this platform's binaries on one platform, such as compiling 32-bit programs on 32-bit platforms and compiling 64-bit programs on 64-bit platforms.

Now 64-bit systems (mostly x86_64 systems, including AMD64 and Intel's EM64T) have been widely used, even if you now want to buy 32-bit processors. The 64-bit system also has a significant improvement in performance and processing capabilities relative to the 32-bit system. So now many users have chosen to use the 64-bit system.

There are some software that requires some code to be compiled during the installation process to work well in different release versions. So when we install 32-bit software on a 64-bit system, its compilation will go wrong because the binary part of the software is 32 bits, but the compiled part is 64 bits, so we need to have our 64-bit system compile 32-bit programs, and we need to compile 32-bit programs by default.

By looking at the data, you can use-m32 to enable GCC to compile 32-bit programs (on the x86_64 system), use the-m elf_i386 parameter to enable 64-bit LD to be compatible with 32-bit libraries, but compile with one of your own commands, so there is no way to give him parameters, But this command is also to invoke GCC and LD, so we can modify GCC and ld to resolve, that is, the two support 32-bit compile connection parameters by default to add:


The/USR/BIN/GCC and/usr/bin/ld were renamed *.bin, which was changed to:/usr/bin/gcc.bin and/usr/bin/ld.bin

Then write two scripts:

/USR/BIN/GCC:

#!/bin/sh

gcc.bin-m32 $@


/USR/BIN/LD:

#!/bin/sh

ld.bin-m elf_i386 $@

This guarantees that the required arguments will be added to the call, and then the real argument ($@ represents all the parameters of the current command, which is to pass all the parameters passed to the textbooks to the real command), is tested to solve the problem.

You can test it with a simple C code:

/* test.c/
#include <stdio.h>
int main ()
{
    printf ("sizeof Long is%d\n", sizeof (long));
    return 0;
}
$ gcc test.c
$ file a.out
a.out:elf 64-bit LSB executable ...
$./a.out
sizeof Long is 8

32-bit compilation execution results (the 64-bit system compiles 32-bit programs need to add-m32 parameters to GCC):

$ gcc-m32 test.c
$ file a.out
a.out:elf 32-bit LSB executable ...
$./a.out
sizeof Long is 4

Also note that if you want to use the-m32 parameter on Ubuntu, you should install the following libraries:

$ sudo apt-get install build-essential module-assistant
$ sudo apt-get install gcc-multilib g++-multilib

If you want to move with the MV command every time you switch between 32-bit compilation and 64-bit compilation, it can be cumbersome to do this with the following script:

If you want 64-bit compilation, then use gcc64.sh

#!/bin/sh

mv-i/usr/bin/gcc/usr/bin/gcc.sh
mv-i/usr/bin/ld  /usr/bin/ld.sh

/ USR/BIN/GCC
mv-i/usr/bin/ld.bin  /usr/bin/ld

If you compile with 32-bit, use gcc32.sh:

#!/bin/sh

mv-i/usr/bin/gcc/usr/bin/gcc.bin
mv-i/usr/bin/ld  /usr/bin/ld.bin

mv-i/usr/bin/ GCC.SH/USR/BIN/GCC
mv-i/usr/bin/ld.sh  /usr/bin/ld


Reference Links:

Http://blog.sina.com.cn/s/blog_542ecc79010007gh.html










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.