{"id":8247,"date":"2022-06-07T14:21:46","date_gmt":"2022-06-07T13:21:46","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=8247"},"modified":"2023-03-03T14:54:02","modified_gmt":"2023-03-03T14:54:02","slug":"dealing-with-multiple-compilers","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2022\/06\/dealing-with-multiple-compilers\/","title":{"rendered":"Dealing with multiple compilers"},"content":{"rendered":"\n<p>I don&#8217;t know you, but when I am compiling a complicated program and everything goes straightforward I feel a mixture of joy and surprise. Let&#8217;s face it, compiling can be quite frustrating, and if you need to compile something relatively old, chances are that you will spend hours and hours trying to understand the compiler error messages.<\/p>\n\n\n\n<p>Several such compiler errors, that in many cases can be quite convoluted, tell you that your program requires an older version, so you first need to install it. I am going to assume that you have sudo rights, otherwise, we will be playing the game of compiling a compiler, something that I recommend you to do at least and at most once in your life. <\/p>\n\n\n\n<p>In common Linux distributions like Ubuntu, installing an older compiler is as easy as using apt or yum:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Ubuntu\n$ sudo apt install build-essential\n$ sudo apt install gcc-7 g++-7<\/code><\/pre>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code\"><code>#Centos\n$ sudo yum install devtoolset-7-gcc*<\/code><\/pre>\n\n\n\n<p>Now, you would have available your previous compiler, which is still set to default, and the new one. In order to use it for your annoying old code, you have basically three options:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Edit the makefile to select the new compiler version<\/li><li>Set an environmental variable with the path of the new compiler<\/li><li>Change your PATH environmental variable to point to the new compiler first<\/li><li>Change the default compiler<\/li><\/ol>\n\n\n\n<p>Options 1 and 2 are equivalent and require you to know the path of the compiler. By default, they are generally installed in <code>\/usr\/bin\/<\/code>. Thus, to compile something using our newly installed g++-7 \/ gcc-7 you only need to run make or cmake with the new compiler pointed in the CC and CXX  variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ make CC=\/usr\/bin\/gcc-7 CXX=\/usr\/bin\/g++-7\n#or\n$ CC=\/usr\/bin\/gcc-7 CXX=\/usr\/bin\/g++-7 cmake YOUR_ARGUMENTS<\/code><\/pre>\n\n\n\n<p>Option 3 is a bit more convoluted but quite useful to know. The PATH environmental variable in Linux OS tells where the executable files should be looked for. In order to prepend some folders you just need to do <code>PATH=\/my\/first\/dir:my\/second\/dir:$PATH <\/code>and if you want to append them use<code> PATH=\/my\/first\/dir:my\/second\/dir:$PATH<\/code>. So when you type in the terminal any name, like <code>cd<\/code> or <code>ls<\/code>, the OS tries to find a file named <code>cd<\/code> in the first directory of the PATH variable, then in the second one and so on, reporting the infamous <code>command not found<\/code> in case it cannot find it in any directory.<\/p>\n\n\n\n<p> PATH, like any environmental variable, can be set for the whole session using export or it can be set for a single command by setting its value prior to command execution. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls \/home\/me\/myBin\nmyCommand\n#myCommand is a compiled program with execution permisions\nPATH=\/home\/me\/myBin:$PATH myCommand\n# or \n$ export PATH=\/home\/me\/myBin:$PATH \n$ myCommand\n\n<\/code><\/pre>\n\n\n\n<p>Lastly, in option 4, you can just change the default version of your compiler using update-alterna<code>tives<\/code> in Ubuntu or <code>scl enable <\/code>in Centos<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#Ubuntu\n##Add the installed version as an alternative\n$ sudo update-alternatives --install \/usr\/bin\/gcc gcc \/usr\/bin\/gcc-7 7\n$ sudo update-alternatives --install \/usr\/bin\/g++ g++ \/usr\/bin\/g++-7 7\n##Check that the aplternative is available\n$ sudo update-alternatives --display gcc\n$ sudo update-alternatives --display g++\n##set the desired version\n$ sudo update-alternatives --config gcc \nThere are 4 choices for the alternative gcc (providing \/usr\/bin\/gcc).\n\n  Selection    Path              Priority   Status\n------------------------------------------------------------\n  0            \/usr\/bin\/gcc-4.8   50        auto mode\n  1            \/usr\/bin\/gcc-4.8   50        manual mode\n  2            \/usr\/bin\/gcc-7     5         manual mode\n  3            \/usr\/bin\/gcc-8     4         manual mode\n* 4            \/usr\/bin\/gcc-9     9         manual mode\n\nPress &lt;enter&gt; to keep the current choice[*], or type selection number: 4\n\n$ sudo update-alternatives --config g++ \nThere are 4 choices for the alternative g++ (providing \/usr\/bin\/g++).\n\n  Selection    Path              Priority   Status\n------------------------------------------------------------\n  0            \/usr\/bin\/g++-4.8   50        auto mode\n  1            \/usr\/bin\/g++-4.8   50        manual mode\n  2            \/usr\/bin\/g++-7     5         manual mode\n  3            \/usr\/bin\/g++-8     4         manual mode\n* 4            \/usr\/bin\/g++-9     9         manual mode\n\nPress &lt;enter&gt; to keep the current choice[*], or type selection number: 4<\/pre>\n\n\n\n<p>And now you should be able to compile your old code.<\/p>\n\n\n\n<p>Once you have finished, please, do not forget to set as the default compiler the original version. Failing to do so, you may face some unexpected errors in the future, especially when your machine gets automatically updated. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t know you, but when I am compiling a complicated program and everything goes straightforward I feel a mixture of joy and surprise. Let&#8217;s face it, compiling can be quite frustrating, and if you need to compile something relatively old, chances are that you will spend hours and hours trying to understand the compiler [&hellip;]<\/p>\n","protected":false},"author":85,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,588,272],"tags":[612,247],"ppma_author":[557],"class_list":["post-8247","post","type-post","status-publish","format-standard","hentry","category-code","category-linux-gnu-linux","category-software-services","tag-compiler-2","tag-linux"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":557,"user_id":85,"is_guest":0,"slug":"ruben","display_name":"Ruben Sanchez-Garcia","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/7301ee01be22b8e41f73a40f0f9a3c1a8fcc9bd35f6ed27763b3827f56e8735c?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/8247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/85"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=8247"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/8247\/revisions"}],"predecessor-version":[{"id":8309,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/8247\/revisions\/8309"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=8247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=8247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=8247"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=8247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}