{"id":26238,"date":"2024-05-03T05:02:24","date_gmt":"2024-05-03T05:02:24","guid":{"rendered":"https:\/\/linuxways.net\/?p=26238"},"modified":"2024-05-03T05:02:24","modified_gmt":"2024-05-03T05:02:24","slug":"install-gcc-compiler-on-ubuntu-24-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/install-gcc-compiler-on-ubuntu-24-04\/","title":{"rendered":"How to Install gcc Compiler on Ubuntu 24.04"},"content":{"rendered":"<p>To translate human language also known as high-level language to low-level language so that it can be read and understood by the machine compiler are used. Several different languages are used for communicating with machines and based on their types compilers also vary.<\/p>\n<p>GCC is a GNU-based compiler that supports multiple programming languages which include C, C++, Objective-C, go, FORTRAN, and more. On Ubuntu 24.04, the GCC compiler is not pre-installed so to install it there are several ways and the choice of method purely relies on the user.<\/p>\n<ul>\n<li><a href=\"#post-26238-_m9mhw3y6w00i\">How to Install gcc Compiler on Ubuntu 24.04<\/a>\n<ul>\n<li><a href=\"#post-26238-_p33fvqr0yt43\">Though Ubuntu&#8217;s default package manager<\/a><\/li>\n<li><a href=\"#post-26238-_2y78fo37rzm\">Through Source File<\/a><\/li>\n<li><a href=\"#post-26238-_2r452rhr7zx9\">Through Build-Essential Package<\/a><\/li>\n<li><a href=\"#post-26238-_bl4dsa2m3h79\">Through Repository<\/a><\/li>\n<li><a href=\"#post-26238-_dsdws0tze98q\">Compiling a C\/C++ Program using GCC compiler on Ubuntu<\/a><\/li>\n<li><a href=\"#post-26238-_pni0lc6tu3o6\">Conclusion<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><a id=\"post-26238-_m9mhw3y6w00i\"><\/a>How to Install gcc Compiler on Ubuntu 24.04<\/h2>\n<p>The GCC compiler on Ubuntu 24.04 provides a complete runtime environment for code compilation and generating executables. Furthermore, it comes with multiple optimization and debugging features that result in improving the performance and efficiency of compiled applications. To install GCC on Ubuntu there are four ways:<\/p>\n<h3><a id=\"post-26238-_p33fvqr0yt43\"><\/a>1: Though Ubuntu&#8217;s default package manager<\/h3>\n<p>The most easy and steadfast way to install GCC is by using the Ubuntu apt package manager by executing:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> <span class=\"kw2\">gcc<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"278\" class=\"wp-image-26287\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-1.png 800w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-1-300x104.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-1-768x267.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>To verify the installation, check the compiler version:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> <span class=\"re5\">--version<\/span><\/div><\/div>\n<h3><a id=\"post-26238-_t5kgqmkjzlnw\"><\/a><strong><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"231\" class=\"wp-image-26298\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-2.png 802w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-2-300x86.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-2-768x221.png 768w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/strong><\/h3>\n<h3><a id=\"post-26238-_2y78fo37rzm\"><\/a>2: Through Source File<\/h3>\n<p>Another way to install the GCC compiler on Ubuntu is by compiling its source file which can be <a href=\"http:\/\/ftp.gnu.org\/gnu\/gcc\">downloaded from GNU archives<\/a>. The significance of using this method is that you can install any older version of the GCC compiler. To download, either visit the index page of GNU and select the version of your choice or copy the download link and then use the wget utility:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">wget<\/span> http:<span class=\"sy0\">\/\/<\/span>ftp.gnu.org<span class=\"sy0\">\/<\/span>gnu<span class=\"sy0\">\/<\/span>gcc<span class=\"sy0\">\/<\/span>gcc-13.2.0<span class=\"sy0\">\/<\/span>gcc-13.2.0.tar.gz<\/div><\/div>\n<p>Here I have downloaded the latest version of the GCC compiler:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"351\" class=\"wp-image-26304\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-3.png 799w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-3-300x132.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-3-768x337.png 768w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/p>\n<p>Now extract the tar file by using the tarball utility:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">tar<\/span> <span class=\"re5\">-xf<\/span> gcc-13.2.0.tar.gz<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"447\" class=\"wp-image-26315\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-4.png 801w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-4-300x167.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-4-768x429.png 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/p>\n<p>Now install the development libraries for MPFR, MPC, and GMP which are required for compiling the source file of GCC. The MPFR is designed for high-level mathematical calculations, the MPC is the library for the arithmetic of complex numbers having high precision, and the GMP is used for arithmetic without limitations on precision:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> libmpfr-dev libgmp3-dev libmpc-dev <span class=\"re5\">-y<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"805\" height=\"295\" class=\"wp-image-26329\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-5.png 805w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-5-300x110.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-5-768x281.png 768w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/><\/p>\n<p>Further, if these libraries are missing then while compiling the source code you will get an error for these libraries as in the image below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"805\" height=\"357\" class=\"wp-image-26347\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-6.png 805w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-6-300x133.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-6-768x341.png 768w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/><\/p>\n<p>Now navigate to the extracted folder directory and execute the configure file to prepare the build environment for compiling the GCC package. Here in the command, there are several options used which are:<\/p>\n<ul>\n<li><strong>v:<\/strong> This enables the verbose mode to have a more detailed output during the configuration process.<\/li>\n<li><strong>build=x86_64-linux-gnu<\/strong>: To specify the system build, this option is used.<\/li>\n<li><strong>host=x86_64-linux-gnu:<\/strong> To specify the system where the complied program will run<\/li>\n<li><strong>target=x86_64-linux-gnu:<\/strong> The option is used while compiling code which creates a code for a different system other than the one they run on. Here it is the same as the system build and host.<\/li>\n<li><strong>prefix=\/usr\/local\/gcc-13.2.0:<\/strong> Assists in setting up the installation directory of the application. This might vary if you are using any other version.<\/li>\n<li><strong>enable-checking=release:<\/strong> To catch any errors while compiling, certain checks are enabled using this option. Here the release type is for setting fewer checks you can set it to developmental for higher checks.<\/li>\n<li><strong>enable-languages=c,c++:<\/strong> The GCC compiler supports various languages but you can only enable the one you intend to use.<\/li>\n<li><strong>disable-multilib:<\/strong> To allow the compiler to build and run programs for multiple target architectures from a single build the multilib is disabled.<\/li>\n<li><strong>program-suffix=-13.2.0:<\/strong> To differentiate between the programs installed from their other versions if installed a prefix of GCC version is added.<\/li>\n<\/ul>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">.<span class=\"sy0\">\/<\/span>configure <span class=\"re5\">-v<\/span> <span class=\"re5\">--build<\/span>=x86_64-linux-gnu <span class=\"re5\">--host<\/span>=x86_64-linux-gnu <span class=\"re5\">--target<\/span>=x86_64-linux-gnu <span class=\"re5\">--prefix<\/span>=<span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>local<span class=\"sy0\">\/<\/span>gcc-13.2.0 <span class=\"re5\">--enable-checking<\/span>=release <span class=\"re5\">--enable-languages<\/span>=c,<span class=\"kw2\">c++<\/span> <span class=\"re5\">--disable-multilib<\/span> <span class=\"re5\">--program-suffix<\/span>=-13.2.0<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"340\" class=\"wp-image-26363\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-7.png 806w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-7-300x127.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-7-768x324.png 768w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/p>\n<p>Now if you are installing the GCC compiler for the first time, you will receive a configuration error of the C++ 11 compiler required for the compilation of the configure file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"807\" height=\"256\" class=\"wp-image-26376\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-8.png 807w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-8-300x95.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-8-768x244.png 768w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/p>\n<p>To resolve this error you have to install a C++ compiler and for that execute:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> <span class=\"kw2\">g++<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"336\" class=\"wp-image-26402\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-9.png 799w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-9-300x126.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-9-768x323.png 768w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/p>\n<p>Now again execute the configuration file for GCC and once the compilation is successful you will see the message of creating make file:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"804\" height=\"296\" class=\"wp-image-26410\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-10.png 804w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-10-300x110.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-10-768x283.png 768w\" sizes=\"auto, (max-width: 804px) 100vw, 804px\" \/><\/strong><\/p>\n<p>Now to make the complied file executable, use the make command, this will take some time:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">make<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"237\" class=\"wp-image-26419\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-11.png 801w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-11-300x89.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-11-768x227.png 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/p>\n<p>Now install The GCC compiler using the make file which consists of all the instructions for installation:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">make<\/span> <span class=\"kw2\">install<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"807\" height=\"393\" class=\"wp-image-26432\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-12.png 807w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-12-300x146.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-12-768x374.png 768w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>local<span class=\"sy0\">\/<\/span>gcc-13.2.0<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span>gcc-13.2.0 <span class=\"re5\">--version<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"241\" class=\"wp-image-26437\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-13.png 806w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-13-300x90.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-13-768x230.png 768w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/p>\n<h3><a id=\"post-26238-_2r452rhr7zx9\"><\/a>3: Through Build-Essential Package<\/h3>\n<p>Another way to install the GCC compiler on Ubuntu is by installing the build-essential package. This is a meta package that is Debian-based and contains the list of packages that are necessary for building a Debian package. The list includes G++ and GCC compilers as well, which serve the purpose of compiling software from source code. To install GCC via build essential execute:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> build-essential<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"344\" class=\"wp-image-26440\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-14.png 808w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-14-300x128.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-14-768x327.png 768w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/p>\n<p>After installing this meta package verify the GCC installation by checking its version:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> <span class=\"re5\">--version<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"246\" class=\"wp-image-26444\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-15.png 799w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-15-300x92.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-15-768x236.png 768w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/p>\n<h3><a id=\"post-26238-_bl4dsa2m3h79\"><\/a>4: Through Repository<\/h3>\n<p>If you are looking to install multiple versions of GCC then you can use the personal package archive repository by toolchain which contains all the versions of GCC. So to add the repository execute:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> add-apt-repository ppa:ubuntu-toolchain-r<span class=\"sy0\">\/<\/span><span class=\"kw3\">test<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"803\" height=\"341\" class=\"wp-image-26449\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-16.png 803w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-16-300x127.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-16-768x326.png 768w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/p>\n<p>To successfully add the repository update the packages list and then install the GCC version of your preference. Here I have installed versions 12 and 11 for illustration:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> gcc-<span class=\"nu0\">12<\/span> g++-<span class=\"nu0\">12<\/span> <span class=\"re5\">-y<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"302\" class=\"wp-image-26455\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-17.png 813w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-17-300x111.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-17-768x285.png 768w\" sizes=\"auto, (max-width: 813px) 100vw, 813px\" \/><\/p>\n<p>Once you have installed GCC add it as an alternative for both GCC and g++. Here in the below command, I have added version 12 followed by version 10. Here you need to set the priority for each version so for version 12 I have kept the priority of 15 and for version 11 the priority is set to 10. Use the same command below just change the version and priority to the desired one:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> update-alternatives <span class=\"re5\">--install<\/span> <span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span><span class=\"kw2\">gcc<\/span> <span class=\"kw2\">gcc<\/span> <span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span>gcc-<span class=\"nu0\">12<\/span> <span class=\"nu0\">15<\/span> <span class=\"re5\">--slave<\/span> <span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span><span class=\"kw2\">g++<\/span> <span class=\"kw2\">g++<\/span> <span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span>g++-<span class=\"nu0\">12<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"160\" class=\"wp-image-26458\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-18.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-18.png 816w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-18-300x59.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-18-768x151.png 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/p>\n<p>Now to select the version of GCC execute the command below and confirm the selection by entering the choice selection number:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> update-alternatives <span class=\"re5\">--config<\/span> <span class=\"kw2\">gcc<\/span><\/div><\/div>\n<p>Here, I have selected the version 11 for use:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"818\" height=\"344\" class=\"wp-image-26462\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-19.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-19.png 818w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-19-300x126.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-19-768x323.png 768w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/p>\n<p>To confirm the selection, check the GCC version by executing:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> <span class=\"re5\">--version<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"815\" height=\"220\" class=\"wp-image-26467\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-20.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-20.png 815w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-20-300x81.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-20-768x207.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><\/p>\n<h2><a id=\"post-26238-_dsdws0tze98q\"><\/a>Compiling a C\/C++ Program using GCC compiler on Ubuntu<\/h2>\n<p>To compile a code either in C or C++ using GCC, just create a file with either .c or .cpp extension and paste your code. Here I have compiled the following simple C++ code using GCC:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"sy0\">\/\/<\/span> Header <span class=\"kw2\">file<\/span> <span class=\"kw1\">for<\/span> input<span class=\"sy0\">\/<\/span>output functions<br \/>\n<br \/>\n<span class=\"co0\">#include &lt;iostream&gt;<\/span><br \/>\n<br \/>\nusing namespace std;<br \/>\n<br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"sy0\">\/\/<\/span> Prints the written text<br \/>\n<br \/>\ncout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Hello and welcome to linuxways.net<span class=\"es1\">\\n<\/span>&quot;<\/span>;<br \/>\n<br \/>\n<span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>;<br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"804\" height=\"315\" class=\"wp-image-26475\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-21.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-21.png 804w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-21-300x118.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-21-768x301.png 768w\" sizes=\"auto, (max-width: 804px) 100vw, 804px\" \/><\/p>\n<p>After saving the file, use the below syntax for compiling the code, here lstdc++ serves the purpose of linker without using it, you will get the linker error:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> <span class=\"sy0\">&lt;<\/span>file-name<span class=\"sy0\">&gt;<\/span>.cpp -lstdc++<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"193\" class=\"wp-image-26479\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-22.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-22.png 806w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-22-300x72.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-22-768x184.png 768w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/p>\n<p>After the code is complied successfully, it will create an output file and see the code output by executing the file with .out extension:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">.<span class=\"sy0\">\/<\/span>a.out<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"803\" height=\"213\" class=\"wp-image-26481\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-23.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-23.png 803w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-23-300x80.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/05\/word-image-26238-23-768x204.png 768w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/p>\n<h2><a id=\"post-26238-_pni0lc6tu3o6\"><\/a>Conclusion<\/h2>\n<p>GCC is a GNU-based compiler that supports several languages and is used for software development, open-source projects, and web servers like Apache and MySQL. There are primarily four ways to install the GCC compiler on Ubuntu 24.04. These include using apt package manager, GCC source file, build-essential meta package, and toolchain repository.<\/p>\n<p>If you do not have any preference for the version then either use the build-essential or apt package. To install later versions of GCC you should use the PPA repository by toolchain. To compile a code using GCC use the <em>gcc<\/em> command along with the correct linker and view the output by executing the output file generated by the compiler.<\/p>","protected":false},"excerpt":{"rendered":"<p>To install the GCC compiler on Ubuntu 24.04, use apt package manager, GCC source file, build-essential meta package, and toolchain repository.<\/p>","protected":false},"author":113,"featured_media":26534,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-26238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/26238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/113"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=26238"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/26238\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/26534"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=26238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=26238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=26238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}