Mac上用VSCode写C++

  • Post author:
  • Post category:其他




Mac上用VSCode写C++

原地址:https://code.visualstudio.com/docs/cpp/config-clang-mac



前期准备

1、安装visual studio code

2、安装 C/C++ 扩展(extension快捷键:shift+command+X)

在这里插入图片描述

测试Clang是否安装成功,在terminal中输入

clang --version

如果没有安装成功,输入如下命令安装command line developer tools

xcode-select --install



使用教程



创建helloworld文件

1、创建空文件夹projects存储所有VS Code projects,再创建子文件夹helloworld,在该子文件夹目录下输入命令打开VS Code.

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .


code .

命令是在当前工作目录下打开VS Code,但该命令不一定可用,可以直接在VS Code中打开文件夹。

接下来会一步步在

.vscode

文件夹下创建三个文件,分别是


  • tasks.json

    (compiler build settings)

  • launch.json

    (debugger settings)

  • c_cpp_properties.json

    (compiler path and IntelliSense settings)

2、创建source code file:新建helloworld.cpp文件

在这里插入图片描述

复制如下代码到cpp文件

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

保存文件(command+S),左侧

File Explorer

(shift+command+E)中列出了文件

在这里插入图片描述



Build helloworld.cpp

1、创建 task.json 文件告诉VS Code如何build/compile程序,调用Clang C++编译器生成源代码的可执行文件。

在主菜单选择

Terminal

>

Configure Default Build Task

,出现如下图的给编译器预定义的build tasks,选择第二个,将自动在 .vscode 文件夹下创建task.json文件并在编辑器中打开。

在这里插入图片描述

用如下内容替代原task.json文件中的内容

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

2、回到helloworld.cpp文件,用快捷键 shift+command+B 或 Terminal > Run Build Task 来运行build task

3、build成功,下方终端窗口会出现类似如下图的结果

在这里插入图片描述

按 + 号新建当前目录下的终端窗口,用 ls 命令查看生成的可执行文件 helloworld 和 debugging 文件helloworld.dSYM

在这里插入图片描述

输入./helloworld命令即可运行程序



Debug helloworld.cpp

1、在主菜单,寻找

Run

>

Add Configuration…

>

C++ (GDB/LLDB)

,再选择下图选项

在这里插入图片描述

VS Code自动创建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": "clang++ - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "clang++ build active file"
    }
  ]
}

2、回到helloworld.cpp文件,用快捷键 fn + f5 或 Run > Start Debugging 开始调试

在这里插入图片描述

注意:在截至March 2019版本的扩展中,用step over执行 cout 时,在最后一个 cout 完成前DEBUG CONSOLE中没有输出。

3、当想追踪某个变量的变化时,在循环中插入断点,在左侧

Watch

窗口点击➕,输入word,即可在循环中观察变量变化。程序执行暂停时,可以用鼠标悬停在变量上方快速查看变量值。



配置重用

.vscode文件目录为 projects/helloworld/.vscode ,所以之后新建projects后要把上述配置的json文件重新复制使用