MATLAB: How to correctly compile MATLAB-generated C/C++ code on macOS High Sierra (10.13.3)

macmatlab coder

Whenever I try to compile MATLAB C/C++ code, whether it be from within MATLAB Coder or outside of it after using MATLAB Coder to generate the source files, I get the following issue:
xcrun clang -c -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMODEL=exportMe -DHAVESTDIO -DUSE_RTMODEL -DUNIX -I/Users/~/MATLAB-Drive/BacktestWS/codegen/exe/exportMe -I/Users/~/MATLAB-Drive/BacktestWS -I/Applications/MATLAB_R2018a.app/extern/include -I/Applications/MATLAB_R2018a.app/simulink/include -I/Applications/MATLAB_R2018a.app/rtw/c/src -I/Applications/MATLAB_R2018a.app/rtw/c/src/ext_mode/common -I/Applications/MATLAB_R2018a.app/rtw/c/ert -o "exportMe_initialize.o" "exportMe_initialize.c"
xcrun clang -c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMODEL=exportMe -DHAVESTDIO -DUSE_RTMODEL -DUNIX -I/Users/~/MATLAB-Drive/BacktestWS/codegen/exe/exportMe -I/Users/~/MATLAB-Drive/BacktestWS -I/Applications/MATLAB_R2018a.app/extern/include -I/Applications/MATLAB_R2018a.app/simulink/include -I/Applications/MATLAB_R2018a.app/rtw/c/src -I/Applications/MATLAB_R2018a.app/rtw/c/src/ext_mode/common -I/Applications/MATLAB_R2018a.app/rtw/c/ert -o "exportMe_terminate.o" "exportMe_terminate.c"
xcrun clang -c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMODEL=exportMe -DHAVESTDIO -DUSE_RTMODEL -DUNIX -I/Users/~/MATLAB-Drive/BacktestWS/codegen/exe/exportMe -I/Users/~/MATLAB-Drive/BacktestWS -I/Applications/MATLAB_R2018a.app/extern/include -I/Applications/MATLAB_R2018a.app/simulink/include -I/Applications/MATLAB_R2018a.app/rtw/c/src -I/Applications/MATLAB_R2018a.app/rtw/c/src/ext_mode/common -I/Applications/MATLAB_R2018a.app/rtw/c/ert -o "exportMe.o" "exportMe.c"
xcrun clang -c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMODEL=exportMe -DHAVESTDIO -DUSE_RTMODEL -DUNIX -I/Users/~/MATLAB-Drive/BacktestWS/codegen/exe/exportMe -I/Users/~/MATLAB-Drive/BacktestWS -I/Applications/MATLAB_R2018a.app/extern/include -I/Applications/MATLAB_R2018a.app/simulink/include -I/Applications/MATLAB_R2018a.app/rtw/c/src -I/Applications/MATLAB_R2018a.app/rtw/c/src/ext_mode/common -I/Applications/MATLAB_R2018a.app/rtw/c/ert -o "rt_nonfinite.o" "rt_nonfinite.c"
xcrun clang -c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMODEL=exportMe -DHAVESTDIO -DUSE_RTMODEL -DUNIX -I/Users/~/MATLAB-Drive/BacktestWS/codegen/exe/exportMe -I/Users/~/MATLAB-Drive/BacktestWS -I/Applications/MATLAB_R2018a.app/extern/include -I/Applications/MATLAB_R2018a.app/simulink/include -I/Applications/MATLAB_R2018a.app/rtw/c/src -I/Applications/MATLAB_R2018a.app/rtw/c/src/ext_mode/common -I/Applications/MATLAB_R2018a.app/rtw/c/ert -o "rtGetNaN.o" "rtGetNaN.c"
xcrun clang -c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMODEL=exportMe -DHAVESTDIO -DUSE_RTMODEL -DUNIX -I/Users/~/MATLAB-Drive/BacktestWS/codegen/exe/exportMe -I/Users/~/MATLAB-Drive/BacktestWS -I/Applications/MATLAB_R2018a.app/extern/include -I/Applications/MATLAB_R2018a.app/simulink/include -I/Applications/MATLAB_R2018a.app/rtw/c/src -I/Applications/MATLAB_R2018a.app/rtw/c/src/ext_mode/common -I/Applications/MATLAB_R2018a.app/rtw/c/ert -o "rtGetInf.o" "rtGetInf.c"
### Creating standalone executable /Users/~/MATLAB-Drive/BacktestWS/exportMe ...
xcrun clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -Wl,-rpath,/Applications/MATLAB_R2018a.app/bin/maci64 -Wl,-rpath,@executable_path -Wl,-rpath,@executable_path//Users/~/MATLAB-Drive/BacktestWS -L"/Applications/MATLAB_R2018a.app/bin/maci64" -o /Users/~/MATLAB-Drive/BacktestWS/exportMe exportMe_initialize.o exportMe_terminate.o exportMe.o rt_nonfinite.o rtGetNaN.o rtGetInf.o -lm
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gmake: *** [/Users/~/MATLAB-Drive/BacktestWS/exportMe] Error 1
Error(s) encountered while building "exportMe":
### Failed to generate all binary outputs.
This was the result of trying to compile something as simple as:
function exportMe
disp('Hello!')
end
Is there something I'm doing wrong, or something to do with the setup of my system that I can fix? Thank you.

Best Answer

The error that _main is missing means that you're trying to compile an executable and no main function was specified. You can hand-write a main function and specify that to MATLAB Coder.
An example main function is also generated by Coder that you can use in place of your hand-written main function.