Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
Using PVS-Studio with the CMake module
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

Using PVS-Studio with the CMake module

Sep 21 2023

Besides the compile_commands.json mode, you can work with PVS-Studio in CMake with a special CMake module. This mode allows you to integrate the analyzer into a CMake-based project in a deeper way. For example, you can set specific goals for the analysis. Thus, you can check only those parts that you need, not the whole project.

Before you start

Make sure you entered the license key (Linux/macOS section). Otherwise, the analysis won't run. You can read more about entering the license here.

How to add a module to the project

The easiest and recommended way to add a module to the project is to use FetchContent for automatic load. You can do it like this:

include(FetchContent)
FetchContent_Declare(
    PVS_CMakeModule
    GIT_REPOSITORY "https://github.com/viva64/pvs-studio-cmake-module.git"
    GIT_TAG        "master" 
)
FetchContent_MakeAvailable(PVS_CMakeModule)
include("${pvs_cmakemodule_SOURCE_DIR}/PVS-Studio.cmake")

Such code will load the Git repository with the module into the generated cache folder. Thus, you'll integrate the analyzer into your project. Note: master is the latest version. If you have problems with it, try to take the latest release tag of the current analyzer's version.

You can load the file with the PVS-Studio.cmake module yourself if you don't want to have unnecessary dependencies on FetchContent.

Don't forget to update the module with the release of a new analyzer's version to avoid problems in its work. In the GIT_TAG parameter, you can specify the master branch to always use the latest version of the module.

How to configure the module

To run the analyzer, CMake module adds a separate target for the build. When you run the build of this target, the analysis will run with the parameters that you specified when adding this target. To add the analysis target, use the pvs_studio_add_target command. For example:

cmake_minimum_required(VERSION 3.5)
project(pvs-studio-cmake-example CXX)

add_executable(example main.cpp)

# Optional:
# include(FetchContent)
# FetchContent_Declare(....)
# FetchContent_MakeAvailable(....)
include(PVS-Studio.cmake)
pvs_studio_add_target(TARGET example.analyze ALL
                      OUTPUT FORMAT json
                      ANALYZE example
                      MODE GA:1,2
                      LOG target.err
                      ARGS -e /path/to/exclude-path)

This small CMake file contains one target to build the executable file and one target to run the analysis. Let's look at the parameters of the pvs_studio_add_target command:

Target options

  • ALL — the analysis is started automatically when the all target is built. So, it will work with each project build;
  • TARGET — the name of the target created for the analysis. To run the analysis, just build this target;
  • ANALYZE — targets to analyze. To also analyze the dependencies of these targets, add the RECURSIVE flag;
  • RECURSIVE — recursively analyze targets dependencies;
  • COMPILE_COMMANDS — use compile_commands.json instead of specifying targets in the ANALYZE option. It works with CMAKE_EXPORT_COMPILE_COMMANDS and available only when you use Makefile or Ninja generators.

Output options

  • OUTPUT — prints the analyzer output into the build log;
  • LOG — the report file. If you don't specify it, the PVS-Studio.log file will be used in the directory with the CMake cache;
  • FORMAT — the report format. In this case, json is an error format with support for multi-file navigation. You can find a list of available formats here (section "Plog Converter utility");
  • MODE — enable diagnostic groups and their levels.

Analysis options

  • PLATFORM - platform name. Available options: win32, x64/win64, linux32, linux64, macos, arm (IAR Embedded Workbench), pic8 (MPLAB XC8), tms (Texas Instruments C6000);
  • PREPROCESSOR - preprocessed file format (clang/visualcpp/gcc);
  • LICENSE — path to the .lic file;
  • CONFIG — path to the .cfg file;
  • CFG_TEXT — contents of the .cfg file;
  • SUPPRESS_BASE — path to the suppress file in the .suppress.json format;
  • KEEP_COMBINED_PLOG — do not delete the combined .pvs.raw file for subsequent processing by the plog-converter utility.

Other options

  • DEPENDS — additional dependencies for the target;
  • SOURCES — list of source files for analysis;
  • BIN — path to pvs-studio-analyzer (macOS/Linux) or CompilerCommandsAnalyzer.exe (Windows);
  • CONVERTER — path to plog-converter (macOS/Linux) or HtmlGenerator.exe (Windows);
  • C_FLAGS — additional flags for the C compiler;
  • CXX_FLAGS — additional flags for the C++ compiler;
  • ARGSadditional arguments for pvs-studio-analyzer/CompilerCommandsAnalyzer.exe;
  • CONVERTER_ARGSadditional arguments for plog-converter/HtmlGenerator.exe .

How to exclude files from analysis

To exclude files from analysis, use the ARGS option by passing the paths through the -e (--exclude-path) flag, as shown in the example above. You can specify absolute, relative paths, or a search mask (glob). Note that relative paths will be expanded in relation to the build directory. This approach allows you, for example, to exclude third-party libraries from the analysis.

How to start the analysis

To start the analysis, build the target added to pvs_studio_add_target. For example, this is how the analysis run looks like for the example above:

cmake --build <path-to-cache-dir> --target example1.analyze

Before the run, all the targets specified for analysis in the ANALYZE parameter are built.

Here you can find examples of the PVS-Studio integration in CMake.