MATLAB Answers

How can I change my current GCC/G++ version to a supported one?

561 views (last 30 days)
I wish to use a supported GCC or G++ compiler to compile my MEX application or MATLAB Compiler component. I would like to know how to change my current GCC/G++ version to a supported one. I would like to change default version of my compiler through "mex -setup".

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Apr 2019
Currently it is not possible to select a supported compiler with "mex -setup" in LINUX and MAC based systems.
As a workaround, please consider using any of the following:
A. Make system's default "gcc" command point to the supported GCC version
1. Open the terminal window in LINUX and execute the command:
$ which gcc
This will provide the symbolic link (softlink) to the default version of GCC.
2. Navigate to the directory which has this softlink.
Change the softlink to point to the version of GCC that you want to use.
For example, for a standard GCC version 4.7 installed (where the compiler command is put at /usr/bin/gcc-4.7),
this can be done using the following command:
$ sudo ln -f -s /usr/bin/gcc-4.7 gcc
Here "gcc" is the name of the softlink determined in step 1.
If you execute the command “ls –l gcc” on the terminal, it will indicate that softlink points to gcc-4.7.
Note: As an alternative to steps 1 and 2 above, you should also be able to use the following one liner:
$ sudo ln -s -f `dirname \`which gcc\``/gcc-4.3 `which gcc`
3. Now open MATLAB and execute the following command:
>> !gcc --version
This command reflects the version of GCC compiler that is currently being used by MATLAB.
If you specify gcc-4.7 as above, it will indicate “gcc-4.7”.
You may also use the verbose mode ("-v" flag) to display detailed build information:
>> mex -v timestwo.c
B. (For MATLAB releases R2013b and before) Specify the supported GCC compiler in the ""
On MATLAB command window, first execute "mex -setup" and then select "1" and finally "y"
which will include output similar to:
/usr/local/MATLAB/R2013b/bin/ is being copied to
Open "/home/<username>/.matlab/R2013b/' with an editor. The compiler is specified with CC='gcc'.
Replace the 'gcc' part with the path of the supported compiler as in:
C. Specify the compiler while using the "mex" command (this overrides the default compiler)
<For MATLAB releases R2014a and after>
Specify the path of the supported compiler in the compiler option (CC or GCC):
>> mex -v GCC='/usr/bin/gcc-4.7' timestwo.c
>> mex -v CC='/usr/bin/gcc-4.7' timestwo.c
<For MATLAB releases R2013b and before>
Specify the path of the supported compiler in the compiler option (CC):
>> mex -v CC='/usr/bin/gcc-4.7' timestwo.c


Sign in to comment.

More Answers (0)