Fix Multiple main Error in Polyspace Platform User Interface
Issue
When you build a project in the Polyspace® Platform user interface, you might see an error related to multiple definitions of the main() function. The error can happen because of several reasons:
A
main()function is automatically added when building tests in the project. Thismain()function invokes test registration functions that register the tests. If the source code added to the project already contains amain()function, the two definitions can conflict during build.If you create a Polyspace Platform project from your build command, by default, all source files used by the build command are pooled into a single project. If the source files are used in creation of separate binaries, multiple source files might contain a
main()function.
Depending on the compiler used, the exact error message might be some variation of the following:
Multiple definition of 'main'main function already defined
Possible Solutions
Depending on the underlying cause of the error, you might apply one or more solutions.
Skip Source Code main During Build
To avoid error because of conflicts between the main() function in your source code and the automatically generated main() function, wrap the main() function in your source code as follows:
#ifndef PSTEST_BUILD
int main(int argc, char **argv) {
// main function contents
}
#endif
PSTEST_BUILD is automatically defined during project build. As a result, the project build bypasses the main function in your source code and prevents the error. The macro is undefined and the main function visible for all other purposes. For instance, the main function is visible during static analysis with Polyspace
Bug Finder™ or Polyspace
Code Prover™.Create Separate Project for Each Binary Created by Build Command
If you create a Polyspace Platform project from a build command, you can modify the default project creation so that one project is created for every binary created from your build command. This would avoid the situation where source files corresponding to different binaries are pooled into a single project.
For more information, see Modularize Polyspace Projects by Using Makefile Builds.