当前位置:网站首页>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:24:00 【51CTO】
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-useIWYU 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 https://github.com/include-what-you-use/include-what-you-use.gitcd 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 change
- Set 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 file
- Compile 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 ~
边栏推荐
- Overflow: the combination of auto and Felx
- 2022年国内云管平台厂商哪家好?为什么?
- Go语言循环语句(第10课下)
- 离线、开源版的 Notion—— 笔记软件Anytype 综合评测
- Readis configuration and optimization of NoSQL (final chapter)
- Analysis of abnormal frequency of minor GC in container environment
- 智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
- Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
- APOC custom functions and procedures
- La 18e Conférence internationale de l'IET sur le transport d'électricité en courant alternatif et en courant continu (acdc2022) s'est tenue avec succès en ligne.
猜你喜欢

World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection

什么是低代码开发?

Go micro tutorial - Chapter 2 go micro V3 using gin and etcd

Embedded software architecture design - function call

居家打工年入800多万,一共五份全职工作,他还有时间打游戏

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection

"Cannot initialize Photoshop because the temporary storage disk is full" graphic solution

防火墙基础透明模式部署和双机热备

开发者,MySQL专栏完更,助你轻松从安装到入门进阶
随机推荐
[glide] cache implementation - memory and disk cache
聊聊异步编程的 7 种实现方式
长城证券开户安全吗 证券账户怎么开通
Height residual method
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
Li Kou today's question -1200 Minimum absolute difference
It's too convenient. You can complete the code release and approval by nailing it!
Task state rollback and data blocking tasks based on check point mechanism
Web game engine
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Hash table
leetcode刷题目录总结
NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
长城证券安全不 证券开户
Start by counting
Years of training, towards Kata 3.0! Enter the safe container experience out of the box | dragon lizard Technology
Configuration instance of Oracle listener server and client
Is it safe for Anxin securities to open an account online? Is the account opening fee charged
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力