电子工程师技术服务社区
公告
登录
|
注册
首页
技术问答
厂商活动
正点原子
板卡试用
资源库
下载
文章
社区首页
文章
使用Keil-MDK一键格式化你的代码
分 享
扫描二维码分享
使用Keil-MDK一键格式化你的代码
IDE
Keil-MDK
IAMLIUBO
关注
发布时间: 2020-03-26
丨
阅读: 2216
## “瞎讲” 大家在平常写Bug的时候有没有什么强迫症呢?比如缩进是用TAB还是空格,空格是用4个还是2个?大括号是否换行?操作符之间是否插入空格等等~ 相信很多人或多或少都会对代码整洁性有一定要求,但是因为是做硬件开发,平常都是使用Keil-MDK、IAR等软件,可能大家会发现没有集成的代码格式化工具,所以有时候手动整理就显得很麻烦了,这里给大家介绍一下AStyle工具,轻松搞定代码格式! ## “起因” 因为最近负责的项目,算是个比较大的工程,所以代码由多个同事分模块编写,然后合并(手工合并,是的没错!),这就导致了一个工程中的代码会出现多种风格,这让我阅读别人代码的时候有点小小的难受?然后不由自主的就会整理一下别人的代码,但是这也不是一个长久之计!而且刚开始是手动整理,然后后来复制别的编辑器中格式化完成后再复制回来,但是总感觉有点麻烦,而且复制到别的编辑器的时候编码还是个问题,有一些中文注释会乱码,有点让我苦不堪言!所以痛下决心,Google了一下…… 结果发现配置一下AStyle就可以,下面是简单记录,方便大家参考。 ## 步骤 ### 了解 开始前,大家先了解下AStyle比较好,官网戳:[这里](http://astyle.sourceforge.net/) 介绍: > Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C++/CLI, Objective‑C, C# and Java programming languages. > > When indenting source code, we as programmers have a tendency to use both spaces and tab characters to create the wanted indentation. Moreover, some editors by default insert spaces instead of tabs when pressing the tab key. Other editors (Emacs for example) have the ability to "pretty up" lines by automatically setting up the white space before the code on the line, possibly inserting spaces in code that up to now used only tabs for indentation. > > The NUMBER of spaces for each tab character in the source code can change between editors (unless the user sets up the number to his liking...). One of the standard problems programmers face when moving from one editor to another is that code containing both spaces and tabs, which was perfectly indented, suddenly becomes a mess to look at. Even if you as a programmer take care to ONLY use spaces or tabs, looking at other people's source code can still be problematic. > > To address this problem, Artistic Style was created – a filter written in C++ that automatically re-indents and re-formats C / C++ / Objective‑C / C++/CLI / C# / Java source files. It can be used from a command line, or it can be incorporated as a library in another program. ### 下载安装 链接:[下载](http://sourceforge.net/projects/astyle/files/) 安装比较简单,直接解压就可以了。 ### Keil-MDK 配置 依次打开 **Tools** -> **Customize Tools Menu**,然后点击新增按钮增加一个用户自定义菜单,如下图: ![配置步骤](https://makingfun.oss-cn-qingdao.aliyuncs.com/OopsWowblogs/AStyle-Keil-MDK/Keil-MDK-AStyle.png) 大家按照上面步骤配置即可,名字大家自己随便起,重要的是**Command**这个选项一定要定位到你解压出来的AStyle.exe 这个可执行文件。 ### 参数配置 我们还需要配置一下参数,主要是为了格式化的代码符合我们的要求,下面先给大家看一下完整参数,然后再给大家简单分析一下,方便你修改成你的风格: ```bash -n !E --style=google --indent=spaces=2 --align-pointer=name --pad-oper --pad-comma --unpad-paren --attach-return-type-decl --convert-tabs --attach-closing-while --indent-switches --max-instatement-indent=60 ``` * -n:不生成备份文件 * !E:格式化当前文件 * --indent=spaces=2 :缩进使用空格,空格个数为2(你可以修改数字为其他的) * --align-pointer=name :\*,&,^靠近变量名称 * --pad-oper:运算符两端插入空格 * --pad-comma :逗号后插入空格,如果启用了上面的选项,此参数也可以省略 * --unpad-paren :取消括号内部或者外部的空格(比如函数名靠近括号) * --attach-return-type-decl :返回值类型与函数名称保持在同一行 * --convert-tabs :将TAB转换为空格 * --attach-closing-while :do while循环的while语句跟第二个大括号保持在同一行 * --indent-switches :switch语句中的case选项,要保持缩进格式 * --max-instatement-indent=60 :一行代码的字符长度,如果超过这个长度会断开并换行 除了像上面一样一个一个参数去修改配置,我们还可以使用默认格式,比如一些大厂的代码格式规范,当然有不合适的地方,你也可以单独修改,AStyle支持下面这个默认格式,可以直接启用: ```bash style=allman style=java style=kr style=stroustrup style=whitesmith style=vtk style=ratliff style=gnu style=linux style=horstmann style=1tbs style=google style=mozilla style=pico style=lisp ``` 我这里就是在Google默认的格式上,稍做了一下改动,当然我这些配置也不是范本,大家还是要根据自己的习惯去配置,以求达到最好的效果,那么什么是最好的效果呢?看着舒服就可以啦! 还有很多参数没有给大家分析,其实大家可以直接查看官方文档,上面都讲的很详细,还有实际例子可以直观的参考,所以还是建议大家先读一下文档,文档地址: [AStyle文档](http://astyle.sourceforge.net/astyle.html) ### 使用 配置好后我们就可以直接使用了,当你看到某些代码不舒服时,别问别想,直接先格式化下就可以! 使用步骤看图: ![使用](https://makingfun.oss-cn-qingdao.aliyuncs.com/OopsWowblogs/AStyle-Keil-MDK/Keil-MDK-AStyle-usage.png) ## 后记 这里我们只配置了一下格式化当前文件一个自定义菜单,其实还可以配置为格式化目录内所有文件的菜单,但是还是不建议大家一下子格式化所有的文件,避免出现一些不必要的麻烦~ 不过你要是坚持要求这样做的话,可以像下面一样配置: ```bash -n "$E*.c" "$E*.h" --style=google --indent=spaces=2 --align-pointer=name --pad-oper --pad-comma --unpad-paren --attach-return-type-decl --convert-tabs --attach-closing-while --indent-switches --max-instatement-indent=60 ``` 上面的参数,会格式化当前文件所在目录内的所有**.c**文件和**.h**文件,不包括子目录~ Enjoy it~ > Post author: IAMLIUBO Post date: Mon Mar 23 2020 16:11:05 GMT+0800 Post link: https://blogs.oopswow.com/2020/03/23/Use-ASytle-format-your-code-with-Keil-MDK/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-ND 4.0 unless stating additionally.Commercial reprint please contact me by email.
原创作品,未经权利人授权禁止转载。详情见
转载须知
。
举报文章
点赞
(
1
)
IAMLIUBO
擅长:工控电子 能源电源 光电显示 设计,制造及服务 智能硬件
关注
评论
(0)
登录后可评论,请
登录
或
注册
相关文章推荐
MK-米客方德推出工业级存储卡
Beetle ESP32 C3 蓝牙数据收发
Beetle ESP32 C3 wifi联网获取实时天气信息
开箱测评Beetle ESP32-C3 (RISC-V芯片)模块
正点原子数控电源DP100测评
DP100试用评测-----开箱+初体验
Beetle ESP32 C3环境搭建
【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏之二
X
你的打赏是对原创作者最大的认可
请选择打赏IC币的数量,一经提交无法退回 !
100IC币
500IC币
1000IC币
自定义
IC币
确定
X
提交成功 ! 谢谢您的支持
返回
我要举报该内容理由
×
广告及垃圾信息
抄袭或未经授权
其它举报理由
请输入您举报的理由(50字以内)
取消
提交