目 录CONTENT

文章目录

C++代码格式化

TalentQ
2025-07-16 / 0 评论 / 0 点赞 / 2 阅读 / 0 字

一、安装 clang-format

sudo apt install clang-format

二、使用 clang-format

clang-format --style=Google -i FILENAME

三、语法糖

将下面脚本写入 ~/.bashrc 中,则可以直接使用 format FILENAME 来格式化代码。

# format
format() {
    if [ $# -ne 1 ]; then
        echo -e "Usage: \n\tformat <filename>\n"
        return 1
    fi

    if [ ! -f "$1" ]; then
        echo "Error: File '$1' not found."
        return 2
    fi

    clang-format --style=Google -i "$1"
    if [ $? -eq 0 ]; then
        echo "✨successful"
    else
        echo "❌failed"
        return 3
    fi
}

0

评论区