本文介绍了如何与CMake一起使用包含什么的工具来检测未使用的标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 该工具 include-what-you-use 可用于检测不需要的标头。我在我的C ++软件项目中使用 CMake 。我该如何指示CMake在我的软件项目的源文件上自动运行包含什么内容?解决方案 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.txtcmake_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 thatthe included header vector is not needed.user@ubuntu:/tmp$ ls ~/helloCMakeLists.txt main.ccuser@ubuntu:/tmp$ mkdir /tmp/builduser@ubuntu:/tmp$ cd /tmp/builduser@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/builduser@ubuntu:/tmp/build$ makeScanning dependencies of target hello[ 50%] Building CXX object CMakeFiles/hello.dir/main.cc.oWarning: 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-2The 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 hellouser@ubuntu:/tmp/build$ ./helloHello 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 viaset(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 thefind_program(), so it should be possible to remove the if statement if(NOT iwyu_path) above. 这篇关于如何与CMake一起使用包含什么的工具来检测未使用的标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 15:04