且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何与CMake一起使用包含什么的工具来检测未使用的标头?

更新时间:2023-10-17 10:52:58

CMake 3.3 引入了新的目标属性 CXX_INCLUDE_WHAT_YOU_USE 可以设置为程序 include-what-you-use 。例如,此 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"找不到程序包括您使用什么)
endif()

set_property(TARGET hello PROPERTY CXX_INCLUDE_WHAT_YOU_USE $ {iwyu_path})

能够构建文件 main.cc

  #include< iostream> 
#include< vector>

int main(){
std :: cout<< Hello World! << std :: endl;
返回0;
}

,同时具有 include-what-you-use 发出警告,表示不需要
包含的标头 vector

  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编译器标识为GNU 4.9.2
-CXX编译器标识为GNU 4.9.2
-检查是否正常工作C编译器:/ usr / bin / cc
-检查是否正常C编译器:/ usr / bin / cc-运行
-检测C编译器ABI信息
-检测C编译器ABI信息-完成
-检测C编译功能
-检测C编译功能-完成
-检查工作的CXX编译器:/ usr / bin / c ++
-检查工作的CXX编译器:/ usr / bin / c ++-工作
-检测CXX编译器ABI信息
-检测CXX编译器ABI信息-完成
-检测CXX编译功能
-检测CXX编译特征-完成
-配置完成
-生成完成
-构建文件已写入:/ tmp / build
user @ ubuntu:/ tmp / build $ make
扫描目标hello的依赖项
[50%]构建CXX对象CMakeFiles / hello.dir / main.cc.o
警告:包括使用情况报告的诊断信息:

/ home / user / hello / main.cc应该添加以下行:

/home/user/hello/main.cc应该删除这些行:
-#include< vector> //第2-2行

/home/user/hello/main.cc的完整包含列表:
#include< iostream> //对于运算符<<,basic_ostream,cout,endl,ostream
-

[100%]链接CXX可执行文件hello
[100%]构建目标hello
user @ ubuntu:/ tmp / build $ ./hello
Hello World!
user @ ubuntu:/ tmp / build $

如果要将自定义选项传递给包括您所使用的内容,例如-mapping_file ,您可以通过


set(iwyu_path_and_options
$ {iwyu_path}
-Xiwyu
--mapping_file = $ {my_mapping})

set_property(目标为
属性CXX_INCLUDE_WHAT_YOU_USE $ {iwyu_path_and_options})

CMake 3.18 引入了新选项 REQUIRED
find_program() ,因此应该可以删除上面的if语句 if(NOT iwyu_path)。 / p>

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?

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)
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})

CMake 3.18 introduced the new option REQUIRED to the find_program(), so it should be possible to remove the if statement if(NOT iwyu_path) above.