I've googled around and couldn't find a clear way to compile c++17 source code on a Red Hat Enterprise Linux 7.5 Developer Workstation.
I've been able to successfully compile C++17 source code on Fedora using the following command:
g++ -std=c++1z main.cpp -o main
I tried the same thing on my Red Hat workstation and received a message that says g++ -std=c++1z is not a recognized command.
Any help or guidance is appreciated.
The Red Hat ® Enterprise Linux ® Developer Program delivers industry-leading developer tools, instructional resources, and an ecosystem of experts to help Linux programmers maximize productivity in building great Linux applications. The developer tools supplied with Red Hat Enterprise Linux are stable and supported for 10 years.
Step #1: Install C/C++ compiler and related tools. If you are using Fedora, Red Hat, CentOS, or Scientific Linux, use the following yum command to install GNU c/c++ compiler: # yum groupinstall 'Development Tools'. If you are using Debian or Ubuntu Linux, type the following apt-get command to install GNU c/c++ compiler: $ sudo apt-get update.
Find hardware, software, and cloud providers―and download container images―certified to perform with Red Hat technologies. The GNU Compiler Collection (GCC), which is the standard compiler on GNU/Linux distributions such as Fedora and Red Hat Enterprise Linux, moved from version 14 to version 17 of C++ in April 2021.
The GNU Compiler Collection (GCC), which is the standard compiler on GNU/Linux distributions such as Fedora and Red Hat Enterprise Linux, moved from version 14 to version 17 of C++ in April 2021. Thus, the -std=gnu++17 command-line option is now used by default.
There are multiple issues you have to deal with.
The simple answer is "don't do that".
Instead of replacing the installed GCC, build and install your own version into a different directory, using the --prefix
argument to the build configuration.
This is really easy to do, it's only a dozen lines of commands. Here's the basic ones, taken from the GCC Wiki:
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
(Just don't forget to replace 4.6.2
with whatever version of GCC that you want.)
For example, if you do ./configure --prefix=/usr/local/mycompany/gcc-8.1.0/
, then the make install
will place the compiler under that directory (/usr/local/mycompany/gcc-8.1.0/
) instead of in /usr/
or /usr/local/
You can further reduce confusion by adding a prefix or suffix to all the executables. That way, there will no ambiguity about which version of g++
you are executing. For example, if you use --program-suffix=-8.1.0
then every call to g++-8.1.0
will clearly be to your new compiler, and g++
will use the system default.
glibc
compatibilityIf you install a new version of GCC in parallel, any applications you compile with that compiler will depend on a newer version of glibc
, which are not guaranteed to be present on other RHEL computers. This breaks one of the advantages of building on RHEL, which is the promise that something that work on your instance will work on all others, and won't break anything else.
If your product is GPL'd then there's an easy solution - just statically link with the required libraries. GCC has compilation flags for that: -static-libstdc++
and -static-libgcc
. If your product isn't GPL, then you'll have to look the the license. IIRC, those libraries hav special dispensation for being distributed as part of a program compiled by GCC, but I am not a lawyer.
Failing that, you can distribute the libraries as shared libraries (.so
files), and have your installation script download and install them to a known location. When building your application, set the linker flags to set the rpath
of the executables to search in that known location for the shared libraries.
Of course, you'll have to comply with the license to distribute those libraries, but that's pretty easy.
Please install CentOS SCL repository:
yum install centos-release-scl
Install C++ support for GCC version 7:
yum install devtoolset-7-gcc-c++ --enablerepo='centos-sclo-rh'
Switch enviroment to this compiler before build:
scl enable devtoolset-7 'bash'
Check current compiler:
which gcc
It was so hard to find that for me.
Source: https://access.redhat.com/documentation/en-us/red_hat_developer_toolset/7/html/user_guide/chap-gcc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With