Main Content

Troubleshoot Compilation and Linking Errors

Run Polyspace® verification on code that builds successfully with your compiler. Once your code builds successfully, set up a Polyspace project in one of these ways:

  • Trace your build system.

    The software creates a project from your build scripts. It sets appropriate Polyspace analysis options to emulate your build options.

  • If you cannot trace your build system, create a Polyspace project manually.

    Add your sources and includes to the project. Change the default analysis options, if required.

For more information, see Configure and Run Analysis.

The following issue occurs more often if you manually set up your project.

Issue

Before verification and detection of run-time errors, Polyspace compiles your code and detects compilation and linking errors. Even if your code builds successfully with your compiler, you still get compilation errors with Polyspace.

Compilation Phase

Compilation Failure

Possible Cause: Deviations from Standard

The Polyspace compiler strictly follows a C or C++ standard. See C standard version (-c-version) and C++ standard version (-cpp-version). If your compiler allows deviation from the Standard, the Polyspace compilation that uses default options cannot emulate your compiler. For instance, your compiler can allow certain non-ANSI® keywords that Polyspace does not recognize by default.

To guarantee absence of certain run-time errors, the default Polyspace compilation strictly follows the standard. Specific compilers allow specific deviations from this standard and follow internal algorithms to compile your code. Without explicit knowledge of your compiler behavior, Polyspace cannot accommodate those deviations. Accommodating these deviations through some arbitrary internal algorithms can compromise the final analysis results, if the Polyspace algorithm does not match your compiler’s algorithm.

Check the error message that caused the compilation failure and see if you can identify some deviation from the standard. The error message shows the line number that caused the compilation failure. If you run verification from the user interface, you can click the error message and navigate to the corresponding line of code.

Solution

Change analysis options to emulate your compiler more closely by manually adjusting these options:

OptionPurpose
Target and Compiler options

Using these predefined options, you can specify your compiler behavior directly and work around known deviations from the standard.

Often, setting Compiler (-compiler) appropriately is enough to emulate your compiler.

Using these options, you can sometimes work around unknown deviations from the standard.

For instance, you can use these options to replace unrecognized keywords from your preprocessed code with closely matching recognized keywords, or remove them completely. Because you do not change your source code, the options allow you to work around compilation errors while keeping your source code intact.

See also View Error Information When Polyspace Analysis Stops.

For specific types of compilation errors, see Troubleshoot Compilation Errors.

If you cannot solve your compilation error, contact MathWorks® Technical Support and provide your compiler name for better support. See Contact Technical Support About Issues with Running Polyspace.

Possible Cause: Linking Errors

Even if a single compilation unit compiles successfully, you get a linking error because of mismatch between two compilation units. For instance, you define the same function in two .c files with different argument or return types.

Common compilation toolchains do not store information about function prototypes during the linking process. Therefore, despite these types of linking errors, the build does not fail. To guarantee absence of certain run-time errors, Polyspace does not continue analysis when such linking errors occur.

Solution

Fix the linking errors that Polyspace detects. Even if your build process allows these errors, you can have unexpected results during run time. For instance, if two function definitions with the same name but conflicting prototypes exist in your code, when you call the function, the result can be unexpected.

When a linking error occurs, the error message shows the location in your file where Polyspace compilation fails. Previous warning messages show the location of the conflicts that lead to the linking error. Using the line numbers in those messages (or by clicking the messages if you run analysis from the user interface), you can navigate to the location of the conflicts in your code.

For instance, in these messages, compilation fails because of conflicting function return types. The failure occurs on line 5 in file2.c when the function is called. The previous warning messages for line 1 in file1.c and line 1 in file2.c show the locations where the conflicts occur.

For specific types of linking errors, see Troubleshoot Compilation Errors.

Possible Cause: Conflicts with Polyspace Function Stubs

Polyspace uses its own implementation of standard library functions for more efficient verification. If your compiler redeclares and redefines a standard library function, you can get a warning or error when you invoke the function.

The error implies that Polyspace found the redeclaration but cannot find the body of your redefined library function. The verification continues to use the Polyspace implementation of the function but provides a warning. If your redefined function has a different signature from the normal signature of the function, the verification stops with an error.

Warnings and errors of this type often refer to the file __polyspace__stdstubs.c. This file contains prototypes for the Polyspace implementation of standard library functions. The file is located in polyspaceroot\polyspace\verifier\cxx\polyspace_stubs\. polyspaceroot is the Polyspace installation folder.

Solution

If you know the location of the file that contains the body of your redefined standard library function, add the file to your verification. For more information, see Fix Errors from Use of Polyspace Header Files.

If you do not have the function body available:

  • If you see a warning of this type, you can ignore the warning. The verification results are based on Polyspace implementations of standard library functions. If your compiler redefinition closely matches the standard library function specifications, the verification results are still applicable for code compiled with your compiler.

  • If you see an error:

    1. Define the macro __polyspace_no_function_name in your project. For instance, if an error occurs because of a conflict with the definition of the sprintf function, define the macro __polyspace_no_sprintf. For information on how to define macros, see Preprocessor definitions (-D).

      The macro disables the use of Polyspace implementations of the standard library function. The software stubs the standard library function like any other undefined function. You do not have an error because of signature mismatch with the Polyspace implementations.

    2. Contact MathWorks Technical Support and provide information about your compiler.

For some standard library functions, such as assert, and memory allocation functions such as malloc and calloc, Polyspace continues to use its own implementations, even if you redefine the function and provide the function body. For more information, see Fix Polyspace Compilation Warnings Related to Assertion or Memory Allocation Functions.