Gcc package for linux

Installing GCC

This page is intended to offer guidance to avoid some common problems when installing GCC, the official installation docs are in the Installing GCC section of the main GCC documentation. N.B. those installation docs refer to the development trunk, the installation instructions for released versions are included in the release sources.

For most people the easiest way to install GCC is to install a package made for your operating system. The GCC project does not provide pre-built binaries of GCC, only source code, but all GNU/Linux distributions include packages for GCC. The BSD-based systems include GCC in their ports collections. For other operating systems the Installing GCC: Binaries page lists some third-party sources of GCC binaries.

If you cannot find suitable binaries for your system, or you need a newer version than is available, you will need to build GCC from source in order to install it.

Building GCC

  • do not run ./configure from within the source directory, this is not supported. You need to run configure from outside the source directory, in a separate directory created for the build (this is a FAQ)
  • if GCC links dynamically to the GMP, MPFR or MPC support libraries then the relevant shared libraries must be in the dynamic linker’s path, both when building gcc and when using the installed compiler (this is also a FAQ)

Support libraries

  • If it provides sufficiently recent versions, use your OS package management system to install the support libraries in standard system locations. For Debian-based systems, including Ubuntu, you should install the packages libgmp-dev, libmpfr-dev and libmpc-dev. For RPM-based systems, including Fedora and SUSE, you should install gmp-devel, mpfr-devel and libmpc-devel (or mpc-devel on SUSE) packages. The packages will install the libraries and headers in standard system directories so they can be found automatically when building GCC.
  • Alternatively, after extracting the GCC source archive, simply run the ./contrib/download_prerequisites script in the GCC source directory. That will download the support libraries and create symlinks, causing them to be built automatically as part of the GCC build process. Set GRAPHITE_LOOP_OPT=no in the script if you want to build GCC without ISL, which is only needed for the optional Graphite loop optimizations.

The difficult way, which is not recommended, is to download the sources for GMP, MPFR and MPC, then configure and install each of them in non-standard locations, then configure GCC with --with-gmp=/some/silly/path/gmp --with-mpfr=/some/silly/path/mpfr --with-mpc=/some/silly/path/mpc, then be forced to set LD_LIBRARY_PATH=/some/silly/path/gmp:/some/silly/path/mpfr:/some/silly/path/mpc/lib in your environment forever. This is silly and causes major problems for anyone who doesn’t understand how dynamic linkers find libraries at runtime. Do not do this. If building GCC fails when using any of the --with-gmp or --with-mpfr or --with-mpc options then you probably shouldn’t be using them.

Читайте также:  Обновить blender astra linux

Configuration

See Installing GCC: Configuration for the full documentation. A major benefit of running srcdir/configure from outside the source directory (instead of running ./configure) is that the source directory will not be modified in any way, so if your build fails or you want to re-configure and build again, you simply delete everything in the objdir and start again.

For example, configuring and building GCC 4.6.2 (with support for C, C++, Fortran and Go) should be as simple as:

tar xzf gcc-4.6.2.tar.gz cd gcc-4.6.2 ./contrib/download_prerequisites cd .. mkdir objdir cd objdir $PWD/../gcc-4.6.2/configure --prefix=$HOME/GCC-4.6.2 --enable-languages=c,c++,fortran,go make make install

The make step takes a long time. If your computer has multiple processors or cores you can speed it up by building in parallel using make -j 2 (or a higher number for more parallelism).

If your build fails and your configure command has lots of complicated options you should try removing options and keep it simple. Do not add lots of configure options you don’t understand, they might be the reason your build fails.

None: InstallingGCC (последним исправлял пользователь JonathanWakely 2017-07-20 19:47:08)

Источник

How to Install GCC on Ubuntu 22.04 | 20.04

LinuxCapable

The GNU Compiler Collection, commonly known as GCC, is a comprehensive and versatile compiler system that supports various programming languages. It has played a vital role in the development and growth of the open-source software ecosystem since its inception in 1987. Below is a detailed introduction to GCC, highlighting its key features, languages supported, and its significance in the software development world.

Key Features of GCC:

  • Cross-Platform: GCC is available for a wide range of operating systems, including Linux, Windows, and macOS. This cross-platform compatibility makes it an excellent choice for developers who work on multiple platforms.
  • Multi-Language Support: GCC supports a multitude of programming languages, including C, C++, Objective-C, Fortran, Ada, D, Go, and many others. This enables developers to use a single compiler for various projects with different language requirements.
  • Optimization: GCC offers various levels of code optimization, allowing developers to fine-tune the performance of their applications. This feature helps in generating efficient code that can run faster and use less memory.
  • Extensions and Language Features: GCC supports many language extensions and features that go beyond the standard specifications. This helps developers take advantage of cutting-edge language features and use them to create more efficient and powerful applications.
  • Active Development and Support: GCC enjoys a strong community of developers and contributors who continually work to improve and expand its features. This ensures that the compiler remains up-to-date and compatible with the latest language standards and operating systems.
Читайте также:  Linux dexp at uh001b

Significance of GCC in Software Development:

GCC has been a crucial tool for open-source software development and is the default compiler for many Linux distributions. It is widely used by developers worldwide to build, optimize, and debug applications. The flexibility offered by GCC, its support for multiple languages, and continuous development efforts have made it an indispensable tool in the software development world.

In the following guide, we will demonstrate how to install GCC on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa with CLI commands using two methods: Ubuntu’s default repository or installing the Ubuntu Toolchain Launchpad PPA that contains the latest versions of all support versions of GCC, including GCC 12, GCC 11, GCC 10, and GCC 9.

Update Ubuntu

Before you begin, update your system to ensure all existing packages are up to date to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade

Method 1: Install GCC with Ubuntu Repository

The first recommended option to install GCC is to install either the GCC package directly or the build-essential package containing GCC and many other essential development tools such as make, g++, and dpkg-dev.

To begin the installation, use the following command.

sudo apt install build-essential

Once installed, verify the installation and check the version using the following command.

Method 2: Install GCC with Ubuntu Toolchain PPA

The next method will install the latest GCC Compiler or alternative versions you may seek from the Ubuntu Toolchain PPA. To import this PPA, run the following command:

sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa -y

After importing the PPA, update your Ubuntu sources list to reflect the changes made by running the following command in your terminal:

To install a specific version of the GCC compiler on your Ubuntu system using the Ubuntu ToolChain PPA, use the following commands in your terminal:

sudo apt install g++-12 gcc-12
sudo apt install g++-11 gcc-11
sudo apt install g++-10 gcc-10

After running the appropriate command for the version you want to install, the GCC compiler will be successfully installed on your Ubuntu system.

Configure Alternative Versions of the GCC Compiler

As a developer or specific user, you may need to install multiple GCC compiler versions. Follow these steps to configure alternative versions of GCC on your Ubuntu system.

Читайте также:  What is pmap linux

First, install the versions of GCC you need. You can install multiple versions of GCC along with G++ using the following command:

sudo apt install gcc-9 g++-9 gcc-10 g++-10 gcc-11 g++-11 g++-12 gcc-12

Once you have installed the necessary versions, use the update-alternatives command to configure the priority of each version. The following example command sets the priority split between GCC 9, GCC 10, GCC 11, and the latest GCC 12.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 80 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 40 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9

The above commands set GCC 12 as the highest priority with a value of 100. However, you can configure the priorities based on your preferences.

To confirm that GCC 12 is the default version on your system, run the following command:

example version output of gcc compiler on ubuntu 22.04 or 20.04 lts

You can reconfigure the default GCC version on your system by using the update-alternatives command. First, use the following command to list the priorities you previously set:

sudo update-alternatives --config gcc

gcc compiler alternative versions command example output on ubuntu 22.04 or 20.04 lts

This command will display a list of installed GCC versions and their priorities. You can then select the default version by entering the corresponding number.

That’s it! You have successfully configured alternative versions of GCC on your Ubuntu system.

Test GCC Compiler: Create a Test Application

To test compiling with GCC, create the famous “Hello World” program in C using any text editor. This tutorial will use nano.

Open the nano text editor and create a new file named hello.c:

Add the following code to the file:

Save the file by pressing CTRL+O, then exit nano by pressing CTRL+X.

To compile the Hello World program, use the following command:

This command compiles the program and generates an executable file named hello .

Next, run the compiled program by entering the following command:

You should see the following output in your terminal:

Hello, World from Linuxcapable.com!

Conclusion

Once installed, GCC can compile and run C and C++ programs on your Ubuntu system. With the addition of the manual pages package, you can also access comprehensive documentation on how to use GCC and its various features. Whether a novice or an experienced developer, having GCC installed on your Ubuntu system is essential for developing and running C and C++ programs.

Источник

Оцените статью
Adblock
detector