zoukankan      html  css  js  c++  java
  • 使用Clang格式化代码

    在写c/c++ 代码的时候,经常需要格式化代码,提高可读性, 这里介绍几种方法方便格式化

    安装工具

    Clang( 附带安装clang-tidy,git-clang-format,clang-tidy-format)

    $clang-format -version
    clang-format version 10.0.0 
    
    which clang-format   
    /usr/bin/clang-format
    
    

    使用

    clang-format  -i -style=file test.cpp
    

    -i 表示覆盖文件,否则会把格式化的文件打印出来
    -style 是风格设置, file 时会在工作目录查找.clang-format 文件,这个文件可以在官网下载,然后自己根据需要修改。

    vscode 集成

    1. 安装c/c++ 插件后,配置settings.json
        // "C_Cpp.errorSquiggles": "Disabled", // 因为有clang的lint,所以关掉
        // "C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序)
        // "C_Cpp.clang_format_path": "/usr/bin/clang-format",
        // "C_Cpp.clang_format_style": "file"
    
    1. 配置save 时进行格式化

    git 集成

    clang 提供了 git-clang-format 的工具, 当stage 文件之后, 执行 git-clang-format 可以格式化代提交的代码, 比起vscode 的自动格式化,这种效率比较高

    git-clang-format                                          
    changed files:
        test/test.cpp
    

    当没有文件stage 时

    $git-clang-format
    clang-format did not modify any files
    
  • 相关阅读:
    mysql面试题1
    vim常用命令总结转
    centos7编译php扩展详细版
    php阻塞模式与非阻塞模式
    Linux 基础入门
    Jenkins搭建
    Git教程 注: 该博客为转载博客!!!
    centos7 安装apache+php
    熟知error_log快速调试
    Centos7yum安装Redis详细教程
  • 原文地址:https://www.cnblogs.com/hustcpp/p/12763226.html
Copyright © 2011-2022 走看看