OCLint

Using OCLint with CMake

This document shows how to apply OCLint to the projects which use CMake as build system.

Background

CMake is able to generate JSON Compilation Database file compile_commands.json with no hassle at all. If a project uses CMake as its build system, then applying OCLint is a quite easy task.

Generating compile_commands.json

Simply add -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to the existing cmake command, like

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/source-root

As a result, the compile_commands.json file is generated in the CMake build folder.

That’s it!

Using compile_commands.json

We can leave the compile_commands.json file here in the build directory, and use -p option to specify this file for oclint.

But in order to use oclint-json-compilation-database, it’s required to copy or link this file to our source directory, for example:

  • copy the file
cp `pwd`/compile_commands.json /path/to/source-root
  • link the file
ln -s `pwd`/compile_commands.json /path/to/source-root

Running OCLint

Now we can use oclint by

oclint -p <build path> <source0> [... <sourceN>]

build path is the path to the folder that contains compile_commands.json.

In addition, we can use oclint-json-compilation-database for code analysis. In this case, simply enter

oclint-json-compilation-database

For advanced usage, detail instructions can be found in oclint and oclint-json-compilation-database manuals respectively.