Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the tool include-what-you-use together with CMake to detect unused headers?

The tool include-what-you-use can be used to detect unneeded headers. I am using CMake for my C++ software project. How can I instruct CMake to run include-what-you-use automatically on the source files of my software project?

like image 813
Erik Sjölund Avatar asked Jun 20 '15 07:06

Erik Sjölund


People also ask

Do you include header files in CMake?

With CMake, adding header include directories to your C++ project is as easy as using your head in football! Heading those C++ include directories is easy with CMake. As you are probably aware, you can include other source files in C++ with the #include pre-processor directive.

How do you use include what you use?

"Include what you use" means this: for every symbol (type, function variable, or macro) that you use in foo.cc, either foo.cc or foo. h should #include a . h file that exports the declaration of that symbol.

Where does CMake look for include files?

cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.

What is IWYU?

Include-What-You-Use (IWYU), as the name implies, means that the Engine's source code only includes the dependencies that it needs to compile. The purpose of IWYU is to avoid including monolithic header files, such as Engine.


1 Answers

CMake 3.3 introduced the new target property CXX_INCLUDE_WHAT_YOU_USE that can be set to the path of the program include-what-you-use. For instance this CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR) add_executable(hello main.cc)  find_program(iwyu_path NAMES include-what-you-use iwyu REQUIRED)  # If using CGAL<3.18, you remove REQUIRED and use # if(NOT iwyu_path) #   message(FATAL_ERROR "Could not find the program include-what-you-use") # endif()  set_property(TARGET hello PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path}) 

is able to build the file main.cc

#include <iostream> #include <vector>  int main() {   std::cout << "Hello World!" << std::endl;   return 0; } 

and at the same time have include-what-you-use give out a warning that the included header vector is not needed.

user@ubuntu:/tmp$ ls ~/hello CMakeLists.txt  main.cc user@ubuntu:/tmp$ mkdir /tmp/build user@ubuntu:/tmp$ cd /tmp/build user@ubuntu:/tmp/build$ ~/cmake-3.3.0-rc2-Linux-x86_64/bin/cmake ~/hello -- The C compiler identification is GNU 4.9.2 -- The CXX compiler identification is GNU 4.9.2 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /tmp/build user@ubuntu:/tmp/build$ make Scanning dependencies of target hello [ 50%] Building CXX object CMakeFiles/hello.dir/main.cc.o Warning: include-what-you-use reported diagnostics:  /home/user/hello/main.cc should add these lines:  /home/user/hello/main.cc should remove these lines: - #include <vector>  // lines 2-2  The full include-list for /home/user/hello/main.cc: #include <iostream>  // for operator<<, basic_ostream, cout, endl, ostream ---  [100%] Linking CXX executable hello [100%] Built target hello user@ubuntu:/tmp/build$ ./hello  Hello World! user@ubuntu:/tmp/build$ 

If you want to pass custom options to include-what-you-use, like for instance --mapping_file you can do it via

set(iwyu_path_and_options     ${iwyu_path}     -Xiwyu     --mapping_file=${my_mapping})  set_property(TARGET hello     PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path_and_options}) 
like image 141
Erik Sjölund Avatar answered Sep 18 '22 04:09

Erik Sjölund