Red hat linux gcc

Chapter 3. GNU Compiler Collection (GCC)

The GNU Compiler Collection , commonly abbreviated GCC , is a portable compiler suite with support for a wide selection of programming languages.

Red Hat Developer Toolset is distributed with GCC 5.3.1 . This version is more recent than the version included in Red Hat Enterprise Linux and provides a number of bug fixes and enhancements.

3.1. GNU C Compiler

3.1.1. Installing the C Compiler

In Red Hat Developer Toolset, the GNU C compiler is provided by the devtoolset-4-gcc package and is automatically installed with devtoolset-4-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”.

3.1.2. Using the C Compiler

scl enable devtoolset-4 'gcc -o output_file source_file. '

This creates a binary file named output_file in the current working directory. If the -o option is omitted, the compiler creates a file named a.out by default.

When you are working on a project that consists of several source files, it is common to compile an object file for each of the source files first and then link these object files together. This way, when you change a single source file, you can recompile only this file without having to compile the entire project. To compile an object file on the command line, run the following command:

scl enable devtoolset-4 'gcc -o object_file -c source_file'

This creates an object file named object_file. If the -o option is omitted, the compiler creates a file named after the source file with the .o file extension. To link object files together and create a binary file, run:

scl enable devtoolset-4 'gcc -o output_file object_file. '

Note that you can execute any command using the scl utility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent. This allows you to run a shell session with Red Hat Developer Toolset gcc as default:

scl enable devtoolset-4 'bash'

Red Hat Developer Toolset’s gcc executable path will begin with /opt . Alternatively, you can use the following command to confirm that the version number matches that for Red Hat Developer Toolset gcc :

Читайте также:  Linux networkmanager wait online service

Some newer library features are statically linked into applications built with Red Hat Developer Toolset to support execution on multiple versions of Red Hat Enterprise Linux. This adds a small additional security risk as normal Red Hat Enterprise Linux errata would not change this code. If the need for developers to rebuild their applications due to such an issue arises, Red Hat will signal this via a security erratum. Developers are strongly advised not to statically link their entire application for the same reasons.

The Red Hat Developer Toolset 4.1 version of GCC supports Cilk+, an extension to the C and C++ languages for parallel programming, which can be enabled using the -fcilkplus option. A runtime library, libcilkrts , is included in this release to support Cilk+. The libcilkrts library has been a part of Red Hat Enterprise Linux since version 7.2, but the package is not included in all supported Red Hat Enterprise Linux releases. To enable dynamic linkage of binaries and libraries built with Red Hat Developer Toolset 4.1 GCC using Cilk+ features on supported Red Hat Enterprise Linux releases that do not contain libcilkrts , install the libcilkrts.so shared library from Red Hat Developer Toolset 4.1 with such binaries or libraries.

Example 3.1. Compiling a C Program on the Command Line

#include int main(int argc, char *argv[])

To compile this source code on the command line by using the gcc compiler from Red Hat Developer Toolset, type:

~]$ scl enable devtoolset-4 'gcc -o hello hello.c'

3.1.3. Running a C Program

When gcc compiles a program, it creates an executable binary file. To run this program on the command line, change to the directory with the executable file and type:

Example 3.2. Running a C Program on the Command Line

Assuming that you have successfully compiled the hello binary file as shown in Example 3.1, “Compiling a C Program on the Command Line”, you can run it by typing the following at a shell prompt:

Источник

Hello World — installing GCC on RHEL 7

In this tutorial, you will install the GNU Compiler Collection 8.2 from the Red Hat Developer Toolset (DTS) and build a simple C++ Hello World application. This tutorial should take less than 30 minutes to complete.

Before you begin, you will need a current Red Hat Enterprise Linux 7 workstation or server subscription that allows you to download software and get updates from Red Hat. If you don’t have an active subscription, register and obtain the RHEL Developer Suite (includes RHEL server) from here.

Читайте также:  Scp no such file or directory linux

If you encounter difficulties at any point, see Troubleshooting and FAQ.

1. Enable necessary software repositories

In this step, you will configure your system to obtain software, including the Red Hat DTS, latest dynamic languages, and open source databases from the Red Hat Software Collection repository by using the command line interface.

You can add or remove software repositories from the command line using the subscription-manager tool as the root user. Use the —list option to view the available software repositories and verify that you have access to RHSCL, which includes DTS:

$ su - # subscription-manager repos --list | egrep devtools

If you don’t see any RHSCL repositories in the list, your subscription might not include it. See Troubleshooting and FAQ for more information.

If you are using a desktop edition of Red Hat Enterprise Linux, change -server- to -desktop- in the following commands:

# subscription-manager repos --enable rhel-server-devtools-7-rpms # subscription-manager repos --enable rhel-7-server-optional-rpms

2. Setup your development environment

In this next step you will use a single command to download and install GCC 8.2, and other development tools that are part of Red Hat Developer Toolset. The length of time this step takes depends on the speed of your Internet connection and your system. With a reasonably fast connection, this step should complete in well under 5 minutes.

To install all components, type:

$ su - # yum install devtoolset-8

If you want to install a subset of components, see the instructions here.

Note: You will still use devtoolset-8 as the name of the software collection in all scl commands. Only the name of the meta-package to be installed by yum changes.

3. Hello World and your first application

Using DTS C++ from the command line

The GNU C++ compiler is run with the command, g++. You need to add DTS to your environment with scl enable in a Terminal window. Also see Permanently adding DTS to your Development Environment for more information.

$ scl enable devtoolset-8 bash

Now create hello.cpp using a text editor such as vi , nano , or gedit with the following content:

#include using namespace std; int main(int argc, char *argv[])

Now compile and run the program:

$ g++ -o hello hello.cpp $ ./hello Hello, Red Hat Developer Program World!

For more information, see the GNU C++ Compiler section of the Red Hat Developer Toolset 8 User Guide.

Читайте также:  Linux адрес текущей папки

Docker-formatted container images

Docker-formatted container images can be used to run Red Hat Developer Toolset components inside virtual software containers, thus isolating them from the host system and allowing for their rapid deployment. For detailed description of the Red Hat Developer Toolset docker-formatted container images and Red Hat Developer Toolset dockerfiles , see Using Red Hat Software Collections Container Images.

Working with the Red Hat Developer Toolset and Software Collection packages

The Red Hat Developer Toolset is delivered as a set of packages in the Red Hat Software Collection. The software packages in RHSCL are designed to allow multiple versions of software to be installed concurrently. To accomplish this, the desired package is added to your runtime environment as needed with the scl enable command. When scl enable runs, it modifies environment variables and then runs the specified command. The environmental changes only affect the command that is run by scl and any processes that are run from that command. The steps in this tutorial run the command bash to start a new interactive shell to work in the updated environment. The changes aren’t permanent. Typing exit will return to the original shell with the original environment. Each time you login, or start a new terminal session, scl enable needs to be run again.

While it is possible to change the system profile to make RHSCL packages part of the system’s global environment, this is not recommended. Doing this can cause conflicts and unexpected problems with other applications because the system version of the package is obscured by having the RHSCL version in the path first.

Permanently adding DTS to your development environment

To make DTS a permanent part of your development environment, you can add it to the login script for your specific user ID. This is the recommend approach for development, because only processes run under your user ID will be affected.

Using your preferred text editor, add the following line to the end of ~/.bashrc :

source scl_source enable devtoolset-8

After logging out and logging back in again, you can verify that the DTS GCC is in your path by running which g++ or g++ —version .

$ which g++ /opt/rh/devtoolset-4/root/usr/bin/g++
$ g++ -v g++ (GCC) 8.2 20181112 (Red Hat 8.2.1-0)

Источник

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