How do I correctly compile MATLAB-generated C/C++ code on macOS High Sierra (10.13.3)?

5 views (last 30 days)
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.

Accepted Answer

Ryan Livingston
Ryan Livingston on 18 Mar 2018
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.
  2 Comments
Frakam
Frakam on 18 Mar 2018
Perfect answer! Thank you very much. Does the disp() function not get compiled to C/C++?
Ryan Livingston
Ryan Livingston on 18 Mar 2018
Edited: Ryan Livingston on 18 Mar 2018
You're welcome. No disp doesn't generate standalone code. If you open the code generation report you should see disp function highlighted in the report. If you hover over it you'll see the info that disp is an extrinsic function.
Additionally, to tell if a function supports code generation you can look in 2 places:
  1. The supported functions list which lists all codegen-compatible functions alphabetically.
  2. Each function's documentation page that supports code generation will have a section under Extended Capabilities called C/C++ Code Generation. For example the disp page does not have this section but the fprintf page does.
To print things in standalone code you can use the MATLAB fprintf function. Coder will compile that to calls to the C printf/fprintf functions.
You can just use:
function exportMe
fprintf('Hello!');
end

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!