Getting started with the Klocwork Desktop C/C++ plugin for Eclipse

Getting started with the Klocwork Desktop C/C++ plugin for Eclipse

The Klocwork plug-in for Eclipse helps you detect and fix issues before check-in.

Information and instructions below also apply to the following C/C++ Eclipse-based IDEs:

  • Wind River Workbench
  • QNX Momentics

If you are a C/C++ developer, you will need to do a bit of tweaking to ensure Klocwork has the build information it needs to run an accurate analysis (more details about this are included in the sections below). The plug-in is equipped with several popular C/C++ refactorings, such as renaming of inlining functions, which can be performed within your IDE.

You can also customize project settings, issue filters and analysis settings to your own preferences. For more information about these features, see the topics below.

Set up a local project

For each coherent, buildable component that you work on, create a local project -- a repository for your analysis results and settings. You need to set up a project only once for each component you work on.

Capture your build settings

Klocwork requires a build specification to perform an analysis. The build specification contains a list of the project's source files and their compiler options (includes and defines). Klocwork creates the build specification by monitoring the IDE build process or by monitoring an external (command-line) build process. The former is recommended as it automatically updates the build specification when files are added or removed from the project.

Important: Do not rely on the Eclipse CDT indexer to generate the build specification, because this method has proven inaccurate.

Note: If you use QNX Momentics, see Capturing your build settings for QNX Momentics instead.

Option 1: If you build outside Eclipse or Wind River Workbench

Run kwinject directly to create a build specification. See Creating a C/C++ build specification.

After you generate your build specification, you need to Specify your build settings.

Option 2: If you build inside Eclipse or Wind River Workbench

The steps vary depending on whether you are using Eclipse, QNX Momentics or Wind River Workbench.

If you are using QNX Momentics, see Capturing your build settings for QNX Momentics.

If you are using Wind River Workbench, see Integrating Klocwork with Wind River Workbench.


  1. Go to Project Properties > C/C++ Build > Tool Chain Editor.
  2. Change the Current Builder for your active configuration from CDT Internal Builder to Gnu Make Builder and click Apply .
  3. Go to Project Properties > C/C++ Build.
  4. In the Builder Settings tab, deselect Use default build command.
  5. In the Build command field, replace your build command with the following:
    <klocwork_user_install>/bin/kwinject -u -o ${ProjDirPath}/kwinject.out <build_command>

    where <build_command> is your build command, for example,make

    See kwinject for an explanation of the options.

  6. Click OK.
  7. To create the build specification, clean and build the project.

    You need to run a "clean" build only once to create the initial build specification in the project directory. After that, when you build, kwinject will update the build specification when files are added or deleted.

Specify your build settings

  1. In the Project Properties, select Klocwork Build Settings.
  2. Select Use build specification file, browse to the build specification you created, and click OK.

The properties are saved for the selected project and used when you run your next analysis. You only have to specify build settings once for your project, as long as your updated build specification file remains in the same location.

Connect to a project on the Klocwork Server

You get the most from static code analysis when you connect a small local project to a larger project on the Klocwork Server. The small local project is analyzed quickly while incorporating Klocwork knowledge bases generated on the server where additional source files were analyzed (for example, shared libraries). Connecting to a server project also allows you to share issue status information with the integration build analysis and among team members. You can run a standalone desktop analysis, but the analysis only derives knowledge from the local project's source files.

You perform this step only once for each project.

  1. In the project properties view navigator, expand Klocwork and select Enable project specific settings.
  2. Click the Synchronization tab.
  3. Ensure that the Klocwork Server host and port information is correct.
  4. In the Project name list, select the server project you want to connect to.
  5. Enable the Use secure connection checkbox if a secure connection to the Klocwork Server has been set up.
  6. Click OK.

    When you set up your connection, synchronization occurs between the integration project and your desktop project, pulling configuration information from the integration project.

    You only have to do this step once for each project.

Import or export your local project synchronization settings

Rather than modify the settings for each project in your workspace, you can perform a batch import/export of all local project synchronization settings for an entire workspace. When exported, the settings are stored in a simple XML file which can be shared with other developers or re-imported to rebuild a workspace.

When imported into a new workspace, all project synchronization settings in the new workspace will be overridden. This is true for all projects, regardless of whether or not the Enable project specific settings checkbox is enabled. If project MyProj in Workspace A has Enable project specific settings disabled, and project MyProj in Workspace B has Enable project specific settings enabled, the configuration settings for project MyProj in Workspace B will be overwritten and local project settings will be disabled.

The configuration settings include:
  • enabling project specific settings
  • the host name and port number of the Klocwork Server
  • secure connection settings
  • the name of the associated project on the server

To export your project synchronization settings

  1. In the Window > Preferences dialog, expand Klocwork and select Settings.
  2. Click Export Map..., then specify a file name and location for your export map file and click Save.
Click View Map to view the configuration mapping file in your default XML viewer. The following listing shows the format of a typical output file with two projects:
    <ProjectSettings EclipseProjectName="myProj"/>
    <ProjectSettings EclipseProjectName="HelloWorld3">
        <ProjectSyncInfo KwProjectName="HelloWorld3" KwServerHost="server11" KwServerPort="8080" KwSslEnabled="true"/>

To import a configuration mapping file

  1. In the Window > Preferences dialog, expand Klocwork and select Settings.
  2. Click Import Map..., then specify the file name and location for the exported map file and click Open.
  3. Click OK to import the configuration map file.
Note: The list of projects specified in the map file can be a subset or superset of the list of projects in your current workspace. At import time, only projects found in the workspace will have their local configuration settings modified. When the import operation completes, a status message is displayed.

Continuous analysis when you open and save files

Continuous analysis is enabled by default and runs when you open or save a file.

If Klocwork issues are detected, an orange square appears in the upper right corner of your editor. Mouse over the orange square to see a summary of the issues detected in your file.

Issues are marked by a lighter orange rectangle on an error stripe to the right, and by an orange chevron to the left. Mouse over either of these icons to see a complete issue message.

To see a list of detected issues, go to Window > Show View > Other > Klocwork > Klocwork Issues.

In connected desktop projects, local issues are those you've introduced since the last integration build analysis; system issues are those that also exist in the integration build.

Investigate detected issues

  1. Double-click an issue in the Issues view to display the Details view.
  2. Use Traceback information to investigate the issue. To see traceback, click or . Traceback lines link to events that contributed to the issue. Clicking a line in the traceback brings up the location in the file where the issue appears, with a bug marker on the left.
  3. Get help on an issue type by right-clicking the issue in the Issues view and selecting More information.

Fix defects and ignore the rest

For a real defect:

  1. Fix the issue in your code and save your changes.
  2. The analysis runs again.
    If the issue disappears from the list, it's fixed.

For an issue that doesn't need to be fixed:

  1. Right-click the issue in the list and select Change Status > Ignore.
  2. Enter a comment and click OK.

If you're connected to a project on the Klocwork Server, your changes are now visible to other developers, and in Klocwork Static Code Analysis. Your desktop project is also updated with any changes made by other developers. This happens even before another integration build analysis has been run.

Discarding issues (CI builds only)

If you configure Continuous Integration (CI) builds and open an issue in Eclipse using the 'open in IDE' button from one of the supported CI plug-ins, this imports the issue into your open project. If you have fixed this issue or simply want to remove it from your issue list, you can right-click on the issue and click Discard imported issue. Note that this functionality does not apply to issues found from a full analysis; these issues cannot be discarded.

Keep your build specification up to date

If you build outside Eclipse, whenever you add files or change build settings, update your build specification with the --update option of kwinject and save it to the same location. If your build specification is out of date, the analysis will not be accurate.