MATLAB: Error using mex on Catalina: “xcodebuild: error: SDK “macosx10.15.4″ cannot be located”

catalinaMATLABmexmexoptssdksimulinkxcode

After upgrading to MacOS Catalina, I am getting the following error when using the "mex" command:
Error using mexxcodebuild: error: SDK "macosx10.15.4" cannot be located.xcrun: error: sh -c '/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk macosx10.15.4 -find clang 2> /dev/null' failed with exit code 16384: (null) (errno=No such file or directory)xcrun: error: unable to find utility "clang", not a developer tool or in PATH
What is the cause of this error and how can I fix it?
 

Best Answer

This is due to an issue where when the "mexopts" files query the SDK version, "macosx10.15.4" is returned when the name of the SDK is actually just "macosx10.15" (major and minor version, no patch number).
In order to work around this, run the following in MATLAB:
>> cd(prefdir)
>> mex -setup % or mex -setup C++
Find the XML files in the Current Folder called "mex_C_maci64.xml" and/or "mex_C++_maci64.xml". Then change the following line:
<SDKVER>  <cmdReturns name="xcrun -sdk macosx --show-sdk-version"/>
</SDKVER>
to 
<SDKVER>
<cmdReturns name="xcrun -sdk macosx --show-sdk-version | awk 'BEGIN {FS=&quot;.&quot;} ; {print $1&quot;.&quot;$2}'"/>
</SDKVER>
Now MATLAB will be able to find the correct SDK. It may require a restart of MATLAB or of the computer.
Please see the following External Bug Report (EBR) for more details: