c++ - How to use the tool include-what-you-use together with CMake to detect unused headers? -
the tool include-what-you-use
can used detect unneeded headers. using cmake c++ software project. how can instruct cmake run include-what-you-use automatically on source files of software project?
cmake 3.3 introduced new target property cxx_include_what_you_use can set path of program include-what-you-use
. instance 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) if(not iwyu_path) message(fatal_error "could not find program include-what-you-use") endif() set_property(target hello property cxx_include_what_you_use ${iwyu_path})
is able build file main.cc
#include <iostream> #include <vector> int main() { std::cout << "hello world!" << std::endl; return 0; }
and @ same time have include-what-you-use
give out warning included header vector 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 -- c compiler identification gnu 4.9.2 -- cxx compiler identification gnu 4.9.2 -- check working c compiler: /usr/bin/cc -- check 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 working cxx compiler: /usr/bin/c++ -- check 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 full include-list /home/user/hello/main.cc: #include <iostream> // 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 want pass custom options include-what-you-use
, instance --mapping_file
can 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})
Comments
Post a Comment