Compile and install GCC in Linux

Source: Internet
Author: User
Tags gmp

Recently, I have studied network programming and found that there are many resources in Unix/Linux, which is convenient to study. Therefore, the first step is to install GCC when installing the relevant development environment recently. I think a lot of people will use the RPM package for installation. There is nothing to say. Here we will introduce the installation from source code compilation, my Installation Platform for fedora11-x86_64.

First, download the source code. I downloaded three packages: gmp-4.3.2.tar.bz2,mpfr-2.4.2.tar.bz2,gcc-4.4.3.tar.bz2. Everyone in GCC knows that GMP and mpfr depend on GCC. The official description is as follows.

GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers.

The mpfr library is a C library for multiple-precision floating-point computations with correct rounding.

In the three packages, install GMP first, then install mpfr, and then install GCC. I don't know if anyone will be confused. here we need to compile so many source codes, including the GCC compiler tool. What should we use to compile these source codes before compiling and installing GCC? I still use GCC... Here is the bootstrap concept in the compilation principle. It is to compile the advanced compiler with a low-level compiler. I'm using a gcc-4.4.0 to compile and install the gcc-4.4.3. In fact, when GCC is compiled, it will be compiled three times (3 stages) by default, that is, after the first compilation is completed, the stage1 compiler will be generated, and then the stage2 compiler will be compiled again using the stage1 compiler, the stage3 compiler is compiled by the stage2 compiler for the third time. Finally, the stage2 compiler and stage3 compiler are compared to determine whether the compilation result is correct.

In Linux, the compilation and installation process mainly includes configure and make. In fact, I have not carefully studied the specific functions. Let's go to the official documentation :(. Here is just a rough introduction. Congfiure will generate makefile based on the parameters specified by the user, and then the make command will compile the entire source code based on makefile.

Before performing any operations, you must first understand the directory structure. In general, three directories are required. decompressing the source code package will generate a source code directory srcdir, which is used to store the target directory objdir of the intermediate file during compilation, and installdir is the installation directory after compilation. It should be noted that objdir cannot be used as a subdirectory of srcdir, or vice versa. Okay. Let's get started.

For GMP, we first use the following command to decompress the source code package.

Tar-xjvf gmp-4.3.2.tar.bz2

In the examples, the formula is "Bzip2". If the package is a. GZ extension, use g to specify the decompression method as gzip. Decompress the package and generate the source code directory gmp_srcdir.

Then go to the target directory gmp_objdir of GMP.

$ CD gmp_objdir

Run the following command to configure the configuration.

$ Gmp_srcdir/configure -- prefix = gmp_installdir -- enable-shared

-- Prefix and -- enable are two common parameters in Configure. Others include -- disable and -- with, which will be used later. -- Prefix = gmp_installdir is used to specify gmp_installdir as the installation directory, and -- enable-shared is used to produce shared libraries.

After configure is successful, makefile will be produced in gmp_objdir. Then compile with the make command.

$ Make

Generally, the make process is successful if the parameter specified by configure is correct. However, verify that the installation package generally provides test cases and uses the following command for testing.

$ Make check

After the test is passed, run the following command to install the SDK.

$ Make install

In this way, the GMP is successfully installed to gmp_installdir.

After the GMP installation is successful, continue to install mpfr, but mpfr depends on GMP, so configure will need the -- with parameter, the command is as follows.

Mpfr_srcdir/configure -- prefix = mpfr_installdir -- With-GMP = gmp_installdir -- enable-shared

In the parameter, -- With-GMP = gmp_installdir is used to specify the installation directory of the GMP package required during the mpfr installation process. The remaining steps are the same as those of GMP.

Finally, install GCC and use the following command for configure.

Gcc_srcdir/configure -- prefix = gcc_install -- enable-threads = POSIX -- enable-shared -- disable-multilib -- disable-checking -- enable-classes ages = C, C ++, java -- With-GMP = gmp_installdir -- With-mpfr = mpfr_installdir

In the parameter -- enable-threads = POSIX specifies POSIX/unix98 as the thread support library, which is very important. -- Disable-checking is used to close some internal checks to reduce Compilation Time. In fact, as described above, you can also add the -- disable-Bootstrap parameter to cancel the 3-stage compilation, which saves a lot of time. However, we do not recommend this for the purpose of correct compilation. The -- enable-languages ages = C, C ++, and Java parameters are used to specify languages supported by the compiler. For the fedora-x86_64 I'm using, it's best to add the -- disable-multilib parameter, otherwise you may encounter GNU/stubs-32.h:
No such file or directory error. This parameter is used to cancel multi-object library compilation, that is, to cancel 32-bit library compilation, because my Linux does not have the files required to compile the 32-bit library...

Similarly, after the configure is correct, make the compilation and then make install the installation. Different platforms and parameters may cause different problems. The best solution to the problem is to check the official instructions... Finally, we wish you a smooth installation :).

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.