当前位置:网站首页>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 ~
边栏推荐
- Which domestic cloud management platform manufacturer is good in 2022? Why?
- 智捷云——元宇宙综合解决方案服务商
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- How to implement a delay queue?
- 【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
- Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
- Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
- Spark 中的 Rebalance 操作以及与Repartition操作的区别
- Is it safe for Bank of China Securities to open an account online?
- 上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
猜你喜欢
VB cannot access database stocks
斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
2022PMP考试基本情况详情了解
Learn more about the basic situation of 2022pmp examination
Difference between redis' memory obsolescence strategy and expiration deletion strategy
Object. Usage of keys()
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
整理混乱的头文件,我用include what you use
随机推荐
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
kaili不能输入中文怎么办???
什么是低代码开发?
Unity interview questions (continuously updated)
周大福践行「百周年承诺」,真诚服务推动绿色环保
C# 更加优质的操作MongoDB数据库
ble HCI 流控机制
一文掌握数仓中auto analyze的使用
Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
网页游戏引擎
The winning rate against people is 84%, and deepmind AI has reached the level of human experts in army chess for the first time
Which domestic cloud management platform manufacturer is good in 2022? Why?
It's too convenient. You can complete the code release and approval by nailing it!
Redis 的内存淘汰策略和过期删除策略的区别
超大规模数仓集群在大型商业银行的落地实践
整理混乱的头文件,我用include what you use