@@ -82,21 +82,32 @@ function compile(solvers, mexdir, fortd, gateways, options)
8282if isempty(compiler_major_version )
8383 compiler_major_version = NaN ; % Failed to get the version number
8484end
85- % Get the major version of GCC corresponding to the libgcc used by the Fortran MEX.
86- gcc_major_version = sscanf(getMexLibgcc().gccVersion, ' %d ' );
87- if isempty(gcc_major_version )
88- gcc_major_version = NaN ; % Failed to get the version number
89- end
85+
9086if contains(compiler_manufacturer , ' gnu' ) % gfortran
9187 % -Wno-missing-include-dirs is needed to suppress the warning about missing include directories
9288 % when Simulink is not installed.
9389 extra_compiler_options = ' -g -Wno-missing-include-dirs -fno-stack-arrays -frecursive' ;
90+
9491 % -ftrampoline-impl=heap instructs the compiler to put the trampolines on the heap instead of the
9592 % stack. This option is available since gcc/gfortran 14. Without this option, executable stacks
9693 % will be generated by the internal subroutines passed as actual arguments. This is related to
9794 % a bug of MATLAB R2025a on Linux, which segfaults when handling Fortran MEX files with internal
9895 % procedures. For details, see the `support_internal_procedures` part below and
9996 % https://fortran-lang.discourse.group/t/implementation-of-a-parametrized-objective-function-without-using-module-variables-or-internal-subroutines/9919?u=zaikunzhang
97+ % First, get the major version of GCC corresponding to the libgcc used by the Fortran MEX.
98+ try
99+ gcc_version = getMexLibgcc().gccVersion;
100+ catch exception
101+ gcc_version = ' ' ;
102+ if verbose
103+ warning(' prima:FailToGetGccVersion' , ' Fail to get the version of libgcc: %s ' , exception .message );
104+ end
105+ end
106+ gcc_major_version = sscanf(gcc_version , ' %d ' );
107+ if isempty(gcc_major_version )
108+ gcc_major_version = NaN ; % Failed to get the version number
109+ end
110+ % Now add -ftrampoline-impl=heap if both the Fortran compiler and libgcc are of version 14+.
100111 if compiler_major_version >= 14 && gcc_major_version >= 14
101112 extra_compiler_options = [extra_compiler_options , ' -ftrampoline-impl=heap' ]; % Note the space
102113 end
0 commit comments