Main Content

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 or coder.updateBuildInfo.

  • Inclusion of external code through the app, on the Custom Code tab, or through code generation configuration parameters CustomSource and CustomInclude.

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.

Errors Caused by Path Length Limit on Windows

When generating C/C++ code on the Microsoft® Windows® platform, errors can occur when the length of the file name and file path exceeds the maximum allowed by Windows. In such cases, the compiler is unable to find required files or directories. To resolve this issue, try one of these solutions:

  • Shorten the file path by moving the project folder closer to the root directory. On the Windows platform, the root directory is indicated by a backslash ( \).

  • Use the Windows command subst to create a virtual drive that maps directly to your project folder. For example, if the path to your project folder is C:\Users\user1\OneDrive - MyBuisness\Documents\Projects\My MATLAB Project, map virtual drive Z: directly to your project folder by typing this command at the Windows command prompt:

    subst z: C:\Users\user1\OneDrive - MyBuisness\Documents\Projects\My MATLAB Project
    After running this command, you can access the My MATLAB Project folder by typing Z:. For more information, see the Microsoft documentation of subst.

  • On Windows 10, Version 1607, and later platforms, enable long path support. See the Microsoft documentation of the maximum path length limitation.

Errors Caused by Outdated C/C++ Compiler Version on Linux

If you run MATLAB on a Linux® platform and your GCC compiler is much older than your MATLAB instance, code generation for certain MathWorks® toolbox functions that use precompiled libraries can fail. Toolbox functions that use precompiled libraries include, but are not limited to, image processing functions such as imfilter (Image Processing Toolbox), bwpack (Image Processing Toolbox), and regionprops (Image Processing Toolbox).

To resolve this issue, update your glibc and/or libstdc++ libraries by upgrading the GCC compiler on your Linux platform.

Related Topics

External Websites