当前位置:网站首页>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-05 01:01: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 https://github.com/include-what-you-use/include-what-you-use.git
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 ~
边栏推荐
- skimage: imread & imsave & imshow
- 资深测试/开发程序员写下无bug?资历(枷锁)不要惧怕错误......
- Single step debugging of master data reading of SAP commerce cloud products
- Hand drawn video website
- There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
- SAP UI5 应用开发教程之一百零六 - 如何提高 SAP UI5 应用路由 url 的可读性试读版
- User login function: simple but difficult
- How to use words to describe breaking change in Spartacus UI of SAP e-commerce cloud
- Leetcode70 (Advanced), 322
- Huawei employs data management experts with an annual salary of 2million! The 100 billion market behind it deserves attention
猜你喜欢
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
测试部新来了个00后卷王,上了年纪的我真的干不过了,已经...
“薪資倒掛”、“畢業生平替” 這些現象說明測試行業已經...
Apifox (postman + swagger + mock + JMeter), an artifact of full stack development and efficiency improvement
每日刷题记录 (十三)
Poap: the adoption entrance of NFT?
skimage: imread & imsave & imshow
Expose testing outsourcing companies. You may have heard such a voice about outsourcing
Hill sort of sorting
The performance of major mainstream programming languages is PK, and the results are unexpected
随机推荐
潘多拉 IOT 开发板学习(RT-Thread)—— 实验4 蜂鸣器+马达实验【按键外部中断】(学习笔记)
Multilingual Wikipedia website source code development part II
The difference between string STR and new string
How to use words to describe breaking change in Spartacus UI of SAP e-commerce cloud
那些一门心思研究自动化测试的人,最后都怎样了?
Oracle case: SMON rollback exception causes instance crash
【大型电商项目开发】性能压测-优化-中间件对性能的影响-40
What if the programmer's SQL data script coding ability is weak and Bi can't do it?
[wave modeling 2] three dimensional wave modeling and wave generator modeling matlab simulation
Basic operations of database and table ----- delete index
Hologres query management and timeout processing
揭露测试外包公司,关于外包,你或许听到过这样的声音
Paxos 入门
107. SAP UI5 OverflowToolbar 容器控件以及 resize 事件处理的一些细节介绍
RB technology stack
Visual explanation of Newton iteration method
What did I pay for it transfer to testing post from confusion to firmness?
Basic concept and usage of redis
Database performance optimization tool
The performance of major mainstream programming languages is PK, and the results are unexpected