当前位置:网站首页>Include what you use to save chaotic header files
Include what you use to save chaotic header files
2022-06-25 13:13:00 【Hua Weiyun】
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 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
边栏推荐
猜你喜欢

Possible problems when idea encounters errors occurred while compiling module (solved)

Optimization of lazyagg query rewriting in parsing data warehouse

指针,它那些不得不说的题目
![[转]以终为始,详细分析高考志愿该怎么填](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[转]以终为始,详细分析高考志愿该怎么填

剑指 Offer II 029. 排序的循环链表

Elemtnui select control combined with tree control to realize user-defined search method

515. Find Largest Value in Each Tree Row

Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token

剑指 Offer 第 1 天栈与队列(简单)

Common colors for drawing
随机推荐
提高排名的 15 个基本 SEO 技巧
Summary of leetcode linked list problem solving skills
Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
JSTL tag: fmt:formatdate tag format Chinese standard time or timestamp
Sword finger offer day 1 stack and queue (simple)
J2EE从入门到入土01.MySQL安装
出手即不凡,这很 Oracle!
Uncover gaussdb (for redis): comprehensive comparison of CODIS
JVM参数解释
.NET in China - What's New in .NET
时间过滤器(el-table)中使用
Jupyter Notebook主题字体设置及自动代码补全
利用cmd(命令提示符)安装mysql&&配置环境
揭秘GaussDB(for Redis):全面对比Codis
Conway's law can not be flexibly applied as an architect?
Connect with the flight book and obtain the user information according to the userid
剑指 Offer II 025. 链表中的两数相加
Event triggered when El select Clear clears content
中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队




