Fix Redefinition and Multiple Definition Build Errors in Polyspace Platform User Interface
R2026bIssue
When you build a Polyspace® Platform project that contains graphical tests, you might see errors that refer to a header file in your source code, even though your code builds without errors outside Polyspace. Depending on the compiler used, the Logs pane displays some variation of the following:
Compilation errors, reporting a type or function in the header file as redefined:
shapes.h:1:34: error: conflicting types for 'rect_t'shapes.h(1): error C2371: 'rect_t': redefinition; different basic typesLink errors, reporting that a global variable or function is defined more than once:
multiple definition of `scale'error LNK2005: scale already defined
The build then completes with a failure message:
Compilation of PSTUnit test code failed.When you build the project, Polyspace
Test™ automatically includes the header files
that define the types used by the function under test. These #include
directives are added to the code generated for each graphical test and do not appear in the
Preamble section of the test. The errors can happen for these
reasons:
Compilation errors — The header file does not have an include guard and is included more than once in the generated test code. This happens when several tests use types defined in the same header file, or when the Preamble section of a test also includes the header file. For instance, tests generated by Polyspace Copilot might include the header file in the Preamble section.
Link errors — The header file defines a global variable or a non-inline function instead of only declaring it. Your source code and the generated test code then both contain the definition. An include guard does not prevent this error.
Possible Solutions
To avoid these errors, ensure that header files used in your project have include guards and do not define global variables or non-inline functions. To identify the header file that is causing the error, look for the file name in the error messages. Depending on the underlying cause of the errors, you might apply one or more solutions.
Add Include Guard to Header File
If the compilation errors report a type or function as redefined, add an include guard to the header file. For instance, consider the header file shapes.h that defines the type rect_t used by the functions under test area() and perimeter():
#ifndef SHAPES_H
#define SHAPES_H
typedef struct { int w; int h; } rect_t;
int area(rect_t r);
int perimeter(rect_t r);
#endif#pragma once at the beginning of the header file.Move Definitions Out of Header File
If the link errors report a global variable or function as defined more than once, move the definition from the header file to a source file in your project. Keep only a declaration in the header file. For instance, suppose that the header file contains these definitions:
int shape_count = 0;
int scale(int x) { return 2 * x; }extern int shape_count;
int scale(int x);Alternatively, declare the function as static in the header file. In this case, each file that includes the header file has its own copy of the function. However, static functions defined in a header file are not supported for graphical test authoring. You can still write tests for callers of the function. See Functions and Data Types Not Supported for Graphical Test Authoring in Polyspace Platform User Interface.
Do not declare global variables as static in the header file. If
you do, each file that includes the header file has its own copy of the variable. You
cannot add such a variable to the inputs or assessments of a graphical test, and code in
scripted test steps uses the test file's copy instead of the copy in your source
code.