ubuntu VSCode编写C/C++配置

VS Code下载

官网下载:code.visualstudio.com

安装C/C++扩展支持

  • Ctrl+Shift+P 输入 install extensions,选择安装C/C++ for Visual Studio Code

设置编辑器基本配置

  • File->Preferences->Settings,打开用户设置。VScode支持选择配置,也支持编辑setting.json文件修改默认配置。

  • editor.fontsize 用来设置字体大小,可以设置editor.fontsize : 14;

  • files.autoSave 这个属性是表示文件是否进行自动保存,推荐设置为onFocusChange–文件焦点变化时自动保存。

  • editor.tabCompletion用来在出现推荐值时,按下Tab键是否自动补全,推荐设置为on

  • editor.codeActionsOnSave中的source.organizeImports属性,这个属性能够在保存时,自动调整 import 语句相关顺序,能够让你的 import 语句按照字母顺序进行排列

  • editor.lineNumbers 设置代码行号,即editor.lineNumbers :true;

参考官网C/C++编辑器指导文档

官网C/C++编辑器指导文档链接

设置头文件目录

默认情况下,只搜索当前源目录、其子目录和某些特定于平台的位置。如果找不到引用的头文件,VSCode会在引用它的每个#include指令下面显示一个绿色的波形符号。将光标放在绿色波浪线的#include指令上,会出现灯泡,点击可打开 c_cpp_properties.json,编辑’browse.path’ 属性,可增加更多头文件目录。

显示成员

变量使用(. or ->)时会自动列出成员列表。

list-members

Code格式化
  1. C/C++扩展支持内置了clang-format工具来格式化代码。没有的话,可以单独在扩展支持中安装Clang-Format。

  2. 右键菜单选择 Format Document (Ctrl+Shift+I) 格式化全文或者 选中一段代码右键选择Format Selection (Ctrl+K Ctrl+F) 来格式化这段代码。

  3. Settings 中还可以配置以下选项
    editor.formatOnSave- 保存时自动格式化Code
    editor.formatOnType - 输入;自动格式化Code

  4. 默认情况下,clang-format的格式是以.clang-format文件形式保存的。如果工作区中找到.clang-format文件,将根据文件中指定的样式应用。如果在未找到.clang-format文件,则将会使用设置中的C_Cpp.clang_format_fallbackStyle配置来确定样式。C_Cpp.clang_format_fallbackStyle默认的格式化样式是“VisualStudio”,它是VisualStudio中默认代码格式化程序的近似形式。可调整成Google。
    C_Cpp.clang_format_fallbackStyle
    VS Code 使用的clang-format并不是正式的 clang-format工具,它包含以下clang-format设置:

     UseTab: (VS Code current setting)
     IndentWidth: (VS Code current setting)
     BreakBeforeBraces: Allman
     AllowShortIfStatementsOnASingleLine: false
     IndentCaseLabels: false
     ColumnLimit: 0
    

    如果要换成其他版本的clang-format工具,可以在设置中的C_Cpp.clang_format_path指定路径。

     "C_Cpp.clang_format_path": "C:\\Program Files (x86)\\LLVM\\bin\\clang-format.exe"
    

    C_Cpp.clang_format_path

clang-format
安装

ubuntu上可以直接安装。VSCode默认内置的clang-format版本与我们自己安装的版本可能不同,必要时也可以替换成我们自己安装的: "C_Cpp.clang_format_path": "/usr/bin/clang-format"

 sudo apt install clang-format
 which clang-format
 > /usr/bin/clang-format

简介

clang-format是基于LibFormat封装的工具,用于格式化代码。目前支持 C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C#代码格式化。

$ clang-format -h
OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.

If no arguments are specified, it formats the code from standard input
and writes the result to the standard output.
If <file>s are given, it reformats the files. If -i is specified
together with <file>s, the files are edited in-place. Otherwise, the
result is written to the standard output.

USAGE: clang-format [options] [<file> ...]

OPTIONS:
  --Werror                   - If set, changes formatting warnings to errors
  --assume-filename=<string> - Override filename used to determine the language.
                               When reading from stdin, clang-format assumes this
                               filename to determine the language.
  --cursor=<uint>            - The position of the cursor when invoking
                               clang-format from an editor integration
  --dry-run                  - If set, do not actually make the formatting changes
  --dump-config              - Dump configuration options to stdout and exit.
                               Can be used with -style option.
  --fallback-style=<string>  - The name of the predefined style used as a
                               fallback in case clang-format is invoked with
                               -style=file, but can not find the .clang-format
                               file to use.
                               Use -fallback-style=none to skip formatting.
  --ferror-limit=<uint>      - Set the maximum number of clang-format errors to emit before stopping (0 = no limit). Used only with --dry-run or -n
  --help                     - Display available options (--help-hidden for more)
  -i                         - Inplace edit <file>s, if specified.
  --length=<uint>            - Format a range of this length (in bytes).
                               Multiple ranges can be formatted by specifying
                               several -offset and -length pairs.
                               When only a single -offset is specified without
                               -length, clang-format will format up to the end
                               of the file.
                               Can only be used with one input file.
  --lines=<string>           - <start line>:<end line> - format a range of
                               lines (both 1-based).
                               Multiple ranges can be formatted by specifying
                               several -lines arguments.
                               Can't be used with -offset and -length.
                               Can only be used with one input file.
  -n                         - Alias for --dry-run
  --offset=<uint>            - Format a range starting at this byte offset.
                               Multiple ranges can be formatted by specifying
                               several -offset and -length pairs.
                               Can only be used with one input file.
  --output-replacements-xml  - Output replacements as XML.
  --sort-includes            - If set, overrides the include sorting behavior determined by the SortIncludes style flag
  --style=<string>           - Coding style, currently supports:
                                 LLVM, Google, Chromium, Mozilla, WebKit.
                               Use -style=file to load style configuration from
                               .clang-format file located in one of the parent
                               directories of the source file (or current
                               directory for stdin).
                               Use -style="{key: value, ...}" to set specific
                               parameters, e.g.:
                                 -style="{BasedOnStyle: llvm, IndentWidth: 8}"
  --verbose                  - If set, shows the list of processed files
  --version                  - Display the version of this program

Coding style
  • 当前开放的code style有: LLVM, Google, Chromium, Mozilla, WebKit.

  • 使用 clang-format 配置code style:
    基于google样式,调整缩进宽度为4个空格:

    clang-format -style="{BasedOnStyle: google, IndentWidth: 4}
    

    基于google样式,导出全部的配置到文件中

    clang-format -style=google -dump-config > .clang-format
    

    打开.clang-format编辑一些配置,形成自己的格式配置文件,放置到工程目录下, 将.clang-format配置到 clang-format

     cd projects/Demo
     clang-format -style=file
    
  • 在VSCode中,已安装了clang-format扩展支持的,则工作目录下的.clang-format (当前编码文件的目录以及父目录)自动生效。

  • clang-format 配置文件中的具体配置项的意义可参考 ClangFormatStyleOptions,以下为几个常见的配置:

配置项说明
IndentWidth: 2缩进使用几个空格
AlignAfterOpenBracket: Align括号内容换行,则换行的内容是否与上一行的左括号对齐。不对齐的话使用默认缩进格式
UseTab: Never代码中tab会被空格替换
BreakBeforeBraces: Allman大括号始终换行
AllowShortIfStatementsOnASingleLine: falseif else 条件语句独立一行,不与执行语句同行
IndentCaseLabels: falsecase 行是否要缩进
ColumnLimit: 0单行字符个数限制,超过则换行。 0表示没有限制。
  • Goodle C++代码一般选择Google风格代码。

  • kernel代码风格可基于LLVM略微调整:

      BasedOnStyle: LLVM
      IndentWidth: 8
      UseTab: Always
      BreakBeforeBraces: Linux
      AllowShortIfStatementsOnASingleLine: false
      IndentCaseLabels: false
    
  • 其他有强制要求的代码风格可逐项根据规范调整。调整配置参考 ClangFormatStyleOptions

参考

官网C/C++编辑器指导文档

vscode C++ 开发配置 google code style

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值