Compiler and Linker Errors
When you generate a library, MEX function, or executable from MATLAB® Coder™, the code generator invokes the C/C++ compiler to build a binary artifact. Build errors can occur during this process. These errors can occur during the compiling stage, or the linking stage, or at other stages of the build. You can view compiling and linking errors and warnings on the Build Logs tab of the code generation report.
The specific error messages and warnings that appear depend on the compiler and toolchain that you use for your platform. To see the current compiler or select a different one, at the command prompt, enter:
mex -setup
Build errors can occur for many different reasons. To diagnose and fix errors, you might have to investigate the error messages listed in your compiler documentation. Following are some commonly occurring issues that can lead to build errors when you generate code.
Failure to Specify a Main Function
Specify a main function to generate a C/C++ executable. If you do not specify a main function, a build error occurs. The main function is contained in a separate main file. When you generate code, MATLAB Coder creates an example main file, but does not automatically use it for compilation. The example main function calls the generated code with mock input values. You must modify the example main or create your own main function for realistic input and output handling.
You can specify the main file as a command-line parameter to the
codegen
command, or in the MATLAB
Coder app, or by using configuration parameters. For more information and
examples, see:
If you want the code generator to automatically use the generated example main file to
build an executable for test purposes, you can set the
GenerateExampleMain
property of the configuration object to
'GenerateCodeAndCompile'
. See Incorporate Generated Code Using an Example Main Function.
Failure to Specify External Code Files
If your code uses external C functions in coder.ceval
, then you
must specify the external files containing those functions or build errors can occur.
You can specify the files as command-line parameters to the codegen
command, or in the MATLAB
Coder app, or by using configuration parameters. For more information and
examples, see:
Errors Caused by External Code
When you introduce external code into the build process, the external code can inject its own errors. You can introduce external code through multiple channels:
External type definitions that you create by using
coder.opaque
that are defined in external header files.Structure type definitions that you create by using
coder.cstructname
that are defined in external header files.Calls to external code by using
coder.ceval
.Specification of external build files to the
codegen
command.Inclusion of external code files by
coder.cinclude
orcoder.updateBuildInfo
.Inclusion of external code through the app, on the Custom Code tab, or through code generation configuration parameters
CustomSource
andCustomInclude
.
This list is not exhaustive. To address errors caused by these methods, you must examine and fix the issues with the external code or decouple the external code from your MATLAB code.