MATLAB Answers


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

Asked by Frakam
on 16 Mar 2018
Latest activity Edited by Ryan Livingston on 18 Mar 2018
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/ -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/ -I/Applications/ -I/Applications/ -I/Applications/ -I/Applications/ -o "exportMe_initialize.o" "exportMe_initialize.c"
xcrun clang -c -isysroot /Applications/ -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/ -I/Applications/ -I/Applications/ -I/Applications/ -I/Applications/ -o "exportMe_terminate.o" "exportMe_terminate.c"
xcrun clang -c -isysroot /Applications/ -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/ -I/Applications/ -I/Applications/ -I/Applications/ -I/Applications/ -o "exportMe.o" "exportMe.c"
xcrun clang -c -isysroot /Applications/ -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/ -I/Applications/ -I/Applications/ -I/Applications/ -I/Applications/ -o "rt_nonfinite.o" "rt_nonfinite.c"
xcrun clang -c -isysroot /Applications/ -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/ -I/Applications/ -I/Applications/ -I/Applications/ -I/Applications/ -o "rtGetNaN.o" "rtGetNaN.c"
xcrun clang -c -isysroot /Applications/ -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/ -I/Applications/ -I/Applications/ -I/Applications/ -I/Applications/ -o "rtGetInf.o" "rtGetInf.c"
### Creating standalone executable /Users/~/MATLAB-Drive/BacktestWS/exportMe ...
xcrun clang++ -arch x86_64 -isysroot /Applications/ -Wl,-rpath,/Applications/ -Wl,-rpath,@executable_path -Wl,-rpath,@executable_path//Users/~/MATLAB-Drive/BacktestWS -L"/Applications/" -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
Is there something I'm doing wrong, or something to do with the setup of my system that I can fix? Thank you.


Sign in to comment.

1 Answer

Answer by Ryan Livingston on 18 Mar 2018
 Accepted 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.


Perfect answer! Thank you very much. Does the disp() function not get compiled to C/C++?
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

Sign in to comment.