当前位置:网站首页>To sort out messy header files, I use include what you use
To sort out messy header files, I use include what you use
2022-07-04 17:29:00 【Huawei cloud developer Alliance】
Abstract : Use include-what-you-use(iwyu/IWYU) Clean up redundant header files , Add necessary header files .
This article is shared from Huawei cloud community 《 use include what you use Save the chaotic header file 》, author : Under the village tree .
background
Face large C/C++ When the project is , Confusing header file references , There is often a feeling of being cut and disordered . Years of project iterations , Demand update . As a result, the header file is not updated in time with the code changes , Become a burden of redundancy . This will increase the compilation time of the overall project . If I don't have a tool to help me organize these header files , I will never change their mind . Fortunately, , There is a tool that can meet our requirements –include what you use.
brief introduction
In all kinds of C/C++ In the coding specification , There are usually a few points mentioned :
Do not include unused header files
Unused header files are included at the same time , Introduced unnecessary dependencies , Increased coupling between modules , As long as the header file is modified , The code will be recompiled .
The header file should contain itself
If a file contains a header file . Want to work properly , You must also include another header file , This will increase the burden of use .
So this is also the meaning of sorting header files .
This is also used include-what-you-use( Hereinafter referred to as IWYU) The purpose of .
install
Attach the home page for this project
https://github.com/include-what-you-use/include-what-you-use
IWYU rely on Clang, And it targets Clang Each version of has its own version . So in installation IWYU When , First you need to determine your Clang What version is it . Because I have not installed Clang, So first install Clang.
Clang Installation
It is recommended to compile the source code directly , Reduce strange errors such as symbol incompatibility
clone llvm Warehouse
git clone https://github.com/llvm/llvm-project.git
Switch to the one you want to compile llvm edition
cd llvm-project
git checkout llvmorg-14.0.0
structure LLVM and Clang
cd llvm-project
mkdir build && cd build
cmake -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -G “Unix Makefiles” …/llvm
make -j48
IWYU install
clone IWYU Warehouse
git clone GitHub - include-what-you-use/include-what-you-use: A tool for use with clang to analyze #includes in C and C++ source files
cd include-what-you-use
Switch to and clang Corresponding branch , Because what was just installed 14.0 Version of , So here we also switch to the corresponding version
git checkeout clang_14
stay IWYU Create folders one level above build
mkdir build && cd build
cmake -G “Unix Makefiles” -DCMAKE_PREFIX_PATH=…/llvm/llvm-project/llvm/ …/include-what-you-use/
CMAKE_PREFIX_PATH Point to the just built llvm Catalog . If the build is successful , You'll see
Start compilation IWYU
cd …/include-what-you-use/
make -j48
Compile successfully
If you want more convenient use iwyu, It can be bin Add environment variables , I am creating a soft link to /usr/bin Under the table of contents
ln -s /the/path/of/include-what-you-use/bin/include-what-you-use /usr/bin/iwyu
If all is well , You can use it iwyu -v To check whether the installation was successful
Use
Here is an introduction to Clion In the environment , How to use IWYU
open Clion->File->Setting->Build,Execution,Deployment page
Set up compiler
In the official documentation , Introduce use IWYU When , Use Clang As a compiler . Discover through personal practice , stay 14.0 Under the version ,Gcc 7.3.1 It can also be used IWYU Of .
stay Toolchains Set in Clang The path of , Because of me Gcc It can also run , So there is no changeSet compilation options
->Cmake Setup page .
Create a new compilation template named Debug-iwyu, The focus is on Cmake options Add -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=iwyux among iwyu Be practical in your environment include-what-you-use Path to executable fileCompile the project
Select the newly created compilation template in the compile project interface Debug-iwyu
OK, Everything is all set. , You can start compiling .The result processing
Here is a very simple example ,main.cpp Contains three unused header files
then iwyu During compilation, we will be reminded that these three header files should be removed
But in real large projects , There must be a lot of reminders , Manually modify one by one . Still a mechanical coolie , So in iwyu In the project , Provides a script that can assist in automatic repair fix_includes.py. You can refer to the usage of the project homepage , To help automatically repair .
- [1] IWYU Home page
- [2] Clang Build guidance
Click to follow , The first time to learn about Huawei's new cloud technology ~
边栏推荐
- What grade does Anxin securities belong to? Is it safe to open an account
- S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
- 矿产行业商业供应链协同系统解决方案:构建数智化供应链平台,保障矿产资源安全供应
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
- 51 single chip microcomputer temperature alarm based on WiFi control
- Readis configuration and optimization of NoSQL (final chapter)
- 关于nacos启动时防火墙开启8848的坑
- 【模板】【luogu P4630】Duathlon 铁人两项(圆方树)
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
猜你喜欢
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
世界环境日 | 周大福用心服务推动减碳环保
什么是低代码开发?
"Cannot initialize Photoshop because the temporary storage disk is full" graphic solution
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
整理混乱的头文件,我用include what you use
基于wifi控制的51单片机温度报警器
What is low code development?
Datakit -- the real unified observability agent
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
随机推荐
安信证券网上开户安全吗 开户收费吗
How to "use" Perl modules in directories that are not in @inc- How do I 'use' a Perl module in a directory not in @INC?
Two methods of MD5 encryption
Learn more about the basic situation of 2022pmp examination
Is it safe to open an account online
leetcode:421. The maximum XOR value of two numbers in the array
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Yanwen logistics plans to be listed on Shenzhen Stock Exchange: it is mainly engaged in international express business, and its gross profit margin is far lower than the industry level
一加10 Pro和iPhone 13怎么选?
R language plot visualization: plot visualizes overlapping histograms and uses geom at the top edge of the histogram_ The rug function adds marginal rug plots
TP configuring multiple databases
什么是低代码开发?
中银证券网上开户安全吗?
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
Image retrieval
【云原生】服务网格是什么“格”?
新享科技发布小程序UniPro小优 满足客户移动办公场景
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
中信证券网上开户安全吗 开户收费吗
ble HCI 流控机制