Mac下vscode配置c/c++开发环境

Mark

记录下vscode配置c/c++环境以备不时只需。

  • 快捷键shift+command+p 选择C/Cpp: Edit configurations将会在.vscode下自动生成c_cpp_properties.json,其内容如下所示:
{
     
    "configurations": [
        {
     
            "name": "Mac",
            "includePath": [
                "/usr/local/include",
                "${workspaceFolder}/**",
                "/Library/Developer/CommandLineTools/usr/include",
                "/Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include",
                "${workspaceRoot}"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
  • 快捷键shift+command+p 依次选择 Tasks: Configure Tasks,选择 Create tasks.json file from templates,选择Others,将会在.vscode下自动生tasks.json,其内容如下所示:
{
     
    "tasks": [
        {
     
            "type": "shell",
            "label": "clang++",
            "command": "/usr/bin/clang++", //使用gcc编译C文件,如果你使用C++开发,改成g++
            "args": [
                "-g",
                "${file}",
                "-std=c++14",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ], // 编译命令参数
            "options": {
     
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
     
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}
  • 快捷键shift+command+p输入Debug: Open launch.json
{
     
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
     
            "name": "(lldb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg", // 配置类型,这里只能为cppdbg
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
            "targetArchitecture": "x86_64", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
            "program": "${fileDirname}/${fileBasenameNoExtension}.out", // 将要进行调试的程序的路径
            "args": [],
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
            "environment": [],
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "MIMode": "lldb",
            "preLaunchTask": "clang++" // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
        }
    ]
}

至此配置完成。

代码格式化-- .clang-format

在工程目录下新建一个.clang-format,其内容如下所示:

---
BasedOnStyle: Webkit

# Allow double brackets such as std::vector>.
Standard: Cpp11

SortIncludes: false

# Keep lines under 100 columns long.
ColumnLimit: 140

# Always break before braces
BreakBeforeBraces: Custom
BraceWrapping:
  AfterClass: true
  AfterControlStatement: true
  AfterEnum: true
  AfterFunction: true
  AfterNamespace: true
  AfterStruct: true
  AfterUnion: true
  BeforeCatch: true
  BeforeElse: true
  IndentBraces: false
  SplitEmptyFunction: false
  SplitEmptyRecord: false
  SplitEmptyNamespace: false

  # Keeps extern "C" blocks unindented.
  AfterExternBlock: false

# Indent case labels.
IndentCaseLabels: true

# Right-align pointers and references
PointerAlignment: Left

# ANGLE likes to align things as much as possible.
AlignOperands: true
AlignConsecutiveAssignments: true

# Use 4 space negative offset for access modifiers
AccessModifierOffset: -4

# TODO(jmadill): Decide if we want this on. Doesn't have an "all or none" mode.
AllowShortCaseLabelsOnASingleLine: false

# Useful for spacing out functions in classes
KeepEmptyLinesAtTheStartOfBlocks: true

# Indent nested PP directives.
IndentPPDirectives: AfterHash

ref

Using Clang in Visual Studio Code

你可能感兴趣的:(C/C++)