Get all include folders from cmake executable

Viewed 514

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.
1 Answers

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.

Related