How to change the default GCC compiler in Ubuntu?
As @Tommy suggested, you should use update-alternatives
.
It assigns values to every software of a family, so that it defines the order in which the applications will be called.
It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc
, and one will be favoured.
To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:
update-alternatives --query gcc
Now, note the priority attributed to gcc-4.4
because you'll need to give a higher one to gcc-3.3
.
To set your alternatives, you should have something like this (assuming your gcc
installation is located at /usr/bin/gcc-3.3
, and gcc-4.4
's priority is less than 50):
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.3 50
--edit--
Finally, you can also use the interactive interface of update-alternatives
to easily switch between versions. Type update-alternatives --config gcc
to be asked to choose the gcc version you want to use among those installed.
--edit 2 --
Now, to fix the CXX environment variable systemwide, you need to put the line indicated by @DipSwitch's in your .bashrc
file (this will apply the change only for your user, which is safer in my opinion):
echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc
update-alternatives --config gcc doesn't change the compiler at all
I solved it. The thing when using cuda is that inside cuda folder you have a symbolic link to gcc. This link toke me everytime to gcc-6 instead of gcc-7. I changed the symbolic link, first removing it from cuda folder and then creating a new one which goes directly to gcc-7. In my case
sudo rm /usr/local/cuda-9.0/bin/gcc
sudo rm /usr/local/cuda-9.0/bin/g++
sudo ln -s /usr/bin/gcc-7 /usr/local/cuda-9.0/bin/gcc
sudo ln -s /usr/bin/g++-7 /usr/local/cuda-9.0/bin/g++
Now the compiler is gcc-7 and not gcc-6.
I was expecting a bad behavior of cuda with gcc-7 but everything is working good.
How to change the default version of gcc man page?
I have found the solution:
Find the location of
gcc-6
man page:$ locate gcc-6 | grep 'man'
/usr/share/man/man1/gcc-6.1.gz
/usr/share/man/man1/x86_64-linux-gnu-gcc-6.1.gzso the location of
gcc-6
man page is/usr/share/man/man1/gcc-6.1.gz
Remove the file
/usr/share/man/man1/gcc.1.gz
if it exist:$ sudo rm /usr/share/man/man1/gcc.1.gz
Create a symbolic link to
/usr/share/man/man1/gcc-6.1.gz
:$ sudo ln -s /usr/share/man/man1/gcc-6.1.gz /usr/share/man/man1/gcc.1.gz
Then run comman:
$ man gcc
You will see the man page version of gcc has changed to gcc-6
.
Sorry for my English.
Are default gcc compiler options gcc version specific or OS specific or both?
Yes, you can generally expect the default flags to be quite different. Fortunately, you don't need to know what the flags are, and you don't need to set them.
Ubuntu and Debian are pretty similar, since Ubuntu is so closely based on Debian. But you will see a variety of different options, since the actual options that GCC uses are fairly technical. You can see them by running the following:
gcc -Q -v -x c -c /dev/null
This asks GCC to compile /dev/null
as a C program (-x c -c /dev/null
) and print out a bunch of developer info (-Q -v
). I ran this with GCC 4.4, 4.6, and 4.8 and got different results. Here are the differences between the options for 4.4 and 4.6 on my machine:
< -falign-loops
< -fargument-alias
> -fdelete-null-pointer-checks
> -fprefetch-loop-arrays
> -fsched-critical-path-heuristic
> -fsched-dep-count-heuristic
> -fsched-group-heuristic
> -fsched-last-insn-heuristic
> -fsched-rank-heuristic
> -fsched-spec-insn-heuristic
> -fshow-column
> -fstrict-volatile-bitfields
> -ftree-forwprop
> -ftree-loop-if-convert
> -ftree-phiprop
> -ftree-pta
> -ftree-slp-vectorize
> -fvar-tracking-assignments
< -mfused-madd
Here are the diffs from version 4.6 to 4.8 on my machine:
> -faggressive-loop-optimizations
> -fgnu-runtime
> -fgnu-unique
< -finline-functions-called-once
> -finline-atomics
> -fira-hoist-pressure
> -fsync-libcalls
> -ftree-coalesce-vars
< -fvect-cost-model
> -mfxsr
> -mlong-double-80
On my machine, GCC uses 80 different options by default when compiling C! The options will also change when you compile C++ or compile on different platforms.
But that's okay. You can basically ignore all of these options and just focus on the most important ones, which are the various -W
warning flags, -O
optimization flags (which are really just shortcuts for a ton of preselected -f
flags and a few -m
—on my computer, -O2
enables 63 additional flags!), and the -g
debug data flag.
And of course, the basic flags, like -std=
, -c
, -o
, -l
, -I
, etc.
I mean, do you really need to know what -fbranch-count-reg
does? Not really.
Choosing a default gcc/g++ compiler
When I remove 5.4, Matlab will not recognize 4.9 which I've installed. How do I get Matlab to recognize gcc/g++ 4.9 as my compiler?
I use Ubuntu (from which you OS is derived) and have had a similar issue. Basically, you can use update-alternative
to switch between gcc
versions on your choice. Here is a link that explains how to do it.
Here is the documentation on update-alternative
, if you need more in-depth knowledge of its functionalities.
Note that uninstalling the default gcc
for your distro (5.4 in your case) is no longer required with this solution: you can switch back to it for you development when not using Matlab. You can even install more than two versions of gcc
if needed.
Hope this solves your issue.
Related Topics
How to Run Elasticsearch 2.1.1 as Root User in Linux MAChine
How to Change the Soname of a Binary Directly
How to Read the Last Line of a Text File into a Variable Using Bash
Ldd Doesn't Work on Dynamically Linked Binary
Iterating Over Lists in Makefiles
Configure and Build Opencv to Custom Ffmpeg Install
What Is Kernel Section Mismatch
Error: Ld.So: Object 'Libgtk3-Nocsd.So.0' from Ld_Preload Cannot Be Preloaded
Shell Script Change Directory with Variable
Run Docker in Ubuntu Live Disk
How to Prefill Command Line Input
Ansible Conditional Based on Stdout of Result