3

How can I get all include directories of my executable? I want to introduce cppcheck and I need to forward all include directories to cppcheck (also the include directories of the interface imported libraries).

So for example I have

add_executable(my_exe main.cpp)
target_link_libraries(my_exe PRIVATE RapidJSON::RapidJSON)

How can I get all corresponding include directories of my_exe, including the RapidJSON ones (e.g. ~/.conan/data/RapidJSON/1.1.0/a/b/package/x/include)?

I tried following without success :-(

get_target_property(ALL_INCLUDES my_exe INTERFACE_INCLUDE_DIRECTORIES) # NOTFOUND
get_target_property(ALL_INCLUDES my_exe INCLUDE_DIRECTORIES) # empty
add_executable(my_exe main.cpp)
target_link_libraries(my_exe PRIVATE RapidJSON::RapidJSON)

# the following line should be adapted so that the variable cppcheck_includes also contains the RapidJSON include directories

set(cppcheck_includes           ${CMAKE_SOURCE_DIR}/includes) 

add_custom_target(cppcheck
        COMMAND cppcheck
        --enable=all
        --std=c++11
        --library=/usr/share/cppcheck/cfg/std.cfg
        --check-config
        ${cppcheck_includes}    
        main.cpp
)

I expect that there is no warning, but there is a warning:

Include file: <rapidjson/document.h> not found.
Totschi
  • 81
  • 2
  • Possible duplicate of [Cppcheck support in CMake](https://stackoverflow.com/questions/48625499/cppcheck-support-in-cmake) – arved Oct 28 '19 at 13:50
  • 1
    It would appear that you *don't have any* include directories for `my_exe`. Have you actually added any via `target_include_directories()` or `include_directories()` calls? Also, can you please show the code that is finding the RapidJSON package? – Kevin Oct 28 '19 at 13:55
  • 1
    You should be able to access `rapidjson` root folder through variable like `${CONAN_RAPIDJSON_ROOT}`, so you should be able to adapt your cmake and add the rapidjson includes – ymochurad Oct 28 '19 at 15:13
  • For Conan usage you need to include the cmake file generated conanbuildinfo.cmake, or, use another generator as cmake_find_package or cmake__paths – uilianries Oct 28 '19 at 17:11
  • @ymochurad is right. More specifically, there's a `${CONAN_INCLUDE_DIRS_RAPIDJSON}` variable defined as detailed [here](https://docs.conan.io/en/latest/reference/generators/cmake.html). Then you just need to concatenate both path into your `cppcheck_includes` variable. – Jean-Mathieu Vermosen Nov 21 '19 at 15:13

2 Answers2

0

In my case I just created a target which runs Cppcheck and uses the compilation database of my project as input - see https://github.com/firewave/mame_regtest/blob/master/cppcheck.cmake.

To generate the compilation database you simply need to add

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

to your CMakeLists.txt.

You could also use the integrated support in CMake but I have no experience with that yet.

Firewave
  • 336
  • 2
  • 11
0

Recent versions of CMake support easy integration of cppcheck. Starting with CMake v3.10 cppcheck can be invoked as CMake target by setting the CMAKE_<LANG>_CPPCHECK variable.

Minimal example CMakeLists.txt that should work on Linux when cppcheck is installed:

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(StaticChecker)

# find installed cppcheck executable on system
find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck)
if(CMAKE_CXX_CPPCHECK)
    list(APPEND CMAKE_CXX_CPPCHECK "--enable=all")
    list(APPEND CMAKE_CXX_CPPCHECK "--std=c++11")
endif()

add_library(some_library lib.cpp)

The static analyzer is automatically invoked within the default cmake build target.