当前位置:网站首页>用include what you use拯救混乱的头文件
用include what you use拯救混乱的头文件
2022-06-25 12:17:00 【华为云】
背景
面对大型C/C++项目的时候,混乱的头文件引用,经常会有一种剪不断理还乱的感觉。长年累月的项目迭代,需求更新。导致头文件未能及时随着代码的变动而更新,成为了冗余的负担。这会增加整体项目的编译时间。如果没有一个工具来帮助我整理这些头文件,我是绝对不会动一丝整改他们的想法。幸运的是,有一个工具可以满足我们的要求–include what you use.
简介
在各种C/C++编码规范中,一般都会提到几点:
禁止包含用不到的头文件
用不到的头文件被包含的同时,引入了不必要的依赖,增加了模块之间的耦合度,只要该头文件修改,代码就会重新编译。
头文件应该自包含
如果一个文件包含某个头文件。想要正常工作,还必须包含另外一个头文件,这会增加使用的负担。
所以这也是整理头文件的意义。
这也是使用include-what-you-use(下称IWYU)的初衷。
安装
附上此项目的主页
https://github.com/include-what-you-use/include-what-you-use
IWYU依赖Clang,而且它针对Clang的各个版本都有自己对应的版本。所以在安装IWYU的时候,首先需确定你的Clang是什么版本。由于本人之前未曾安装过Clang,所以首先要安装Clang.
Clang的安装
建议直接源码编译,减少符号不兼容等奇怪的错误
克隆llvm仓库
git clone https://github.com/llvm/llvm-project.git
切到想要编译的llvm版本
cd llvm-project
git checkout llvmorg-14.0.0
构建LLVM和Clang
cd llvm-project
mkdir build && cd build
cmake -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -G “Unix Makefiles” …/llvm
make -j48
IWYU安装
克隆IWYU仓库
git clone https://github.com/include-what-you-use/include-what-you-use.git
cd include-what-you-use
切换到与clang对应的分支,由于刚才安装的是14.0的版本,因此这里也切换到对应版本
git checkeout clang_14
在IWYU上一层创建文件夹build
mkdir build && cd build
cmake -G “Unix Makefiles” -DCMAKE_PREFIX_PATH=…/llvm/llvm-project/llvm/ …/include-what-you-use/
CMAKE_PREFIX_PATH指向刚才构建的llvm目录。如果构建成功,你会看到
开始编译IWYU
cd …/include-what-you-use/
make -j48
编译成功
如果希望更方便的使用iwyu,可以将其bin加入环境变量中,我是创建了一个软链接到/usr/bin目录下
ln -s /the/path/of/include-what-you-use/bin/include-what-you-use /usr/bin/iwyu
如果一切妥当,就可以用iwyu -v来检查是否安装成功了
使用
这里介绍一下在Clion环境下,如何使用IWYU
打开Clion->File->Setting->Build,Execution,Deployment页面
设置编译器
在官方文档中,介绍使用IWYU的时候,使用Clang作为编译器。通过亲自实践发现,在14.0版本下,Gcc 7.3.1也是可以使用IWYU的。
在Toolchains中设置Clang的路径,由于我Gcc也可以运行,所以并没有更改
设置编译选项
->Cmake设置页面。
新建一个编译模板名为Debug-iwyu,重点是在Cmake options中添加 -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=iwyux 其中iwyu为你环境中实际include-what-you-use可执行文件的路径
编译项目
在编译项目的界面选择刚刚创建的编译模板Debug-iwyu
OK,一切准备就绪,就可以开始编译了。结果处理
这里举了一个很简单的例子,main.cpp包含了三个用不到的头文件
然后iwyu在编译的过程中会提醒我们这三个头文件应该被移除
但是真实的大型项目中,肯定会有很多的提醒,一个一个去手动修改。依然是个机械的苦力活,所以在iwyu项目中,提供了一个可以辅助自动修复的脚本fix_includes.py。可以参照项目主页的使用方法,来帮助自动修复。
- [1] IWYU主页
- [2] Clang构建指导
边栏推荐
- (3) Pyqt5 tutorial -- > signal and slot preliminary test
- Jeecgboot startup popup configuration is still incorrect
- Laravel is not logged in and cannot access the background by entering the URL
- Slice() and slice() methods of arrays in JS
- 聊聊高可用的 11 个关键技巧
- torch. Tensor splicing and list (tensors)
- Select randomly by weight [prefix and + dichotomy + random target]
- Idea2017 how to set not to automatically open a project at startup
- Concat(), join(), reverse(), sort() method in JS array
- (7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)
猜你喜欢

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

mysql FIND_ IN_ Set function

3+1保障:高可用系统稳定性是如何炼成的?

First acquaintance with CANopen

Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"

The editor is used every day. What is the working principle of language service protocol?

MySQL and excel tables importing database data (Excel for MySQL)

为何数据库也云原生了?

Singleton mode in PHP to reduce memory consumption

Zhangxiaobai's road of penetration (VI) -- the idea and process of SQL injection and the concat series functions and information of SQL_ Schema database explanation
随机推荐
node. JS architecture optimization: reverse proxy and cache service
5 kinds of viewer for browser
You can't specify target table 'xxx' for update in from clause
(3) Pyqt5 tutorial -- > signal and slot preliminary test
JS picture switching (simple and practical)
又是被Visdom搞崩溃的一夜
Using CMD (command prompt) to install MySQL & configure the environment
MySQL and excel tables importing database data (Excel for MySQL)
The push(), pop(), unshift(), shift() method in the JS array
2021-09-28
地理空间搜索 ->R树索引
Negative sample image used in yolov5 training
QT TCP UDP network communication < theory >
Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection
【AI助力科研】loss曲线傻瓜式绘制
3+1 guarantee: how is the stability of the highly available system refined?
(7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)
利用cmd(命令提示符)安装mysql&&配置环境
Maximum number [abstract rules for abstract sorting]
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1




