0%

让 M1 Mac优雅地编写C/C++

M1 Mac OS 编写C/C++

安装并配置

VS code

Visual Studio Code(简称“VS Code” )是Microsoft在2015年4月30日Build开发者大会上正式宣布一个运行于 Mac OS 、Windows和 Linux之上的,针对于编写现代Web和云应用的跨平台源代码编辑器。

本机配置

电脑配置

为了运行

  • 进入官网,下载

    Universal版本同时适用于Intel芯片 与 苹果芯片(M1)的电脑

VS code下载

  • 汉化插件(自动弹出,点击下载即可)

  • 其他插件(如下图,搜索下载)

    其他插件下载

    • C/C++

    • Code Runner

    • CodeLLDB

      下载完成后,点击其右下角小齿轮进行设置

      CodeLLDB设置

      • 勾选Code-runner: Clear Previous Output
      • 勾选Code-runner: Ignore Selection
      • 取消勾选Code-runner: Preserve Focus
      • 勾选Code-runner: Run In Terminal
      • 勾选Code-runner: Save All Files Before Run
      • 勾选Code-runner: Save File Before Run
  • 创建 纯英文 文件夹(专门放与C相关的)

  • 打开该文件夹 & 信任该文件夹

    打开文件夹

  • 新建 hello.c 文件

    新建.c文件

  • 设置代码自动保存

    • 点开对应设置

    设置

    • 改为afterDelay

      设置自动保存

  • 测试运行

    • hello.c中输入

      1
      2
      3
      4
      5
      #include <stdio.h>
      int main()
      {
      printf("Hello,world!");
      }
    • 点击右上角箭头来运行

      运行

    • 如图,则运行环节完成

      效果图

为了调试

  • hello.c打开调试界面(或直接按下图左边的齿轮),打开launch.json

    调试界面

  • 改launch.json如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    {
    "version": "2.0.0",
    "tasks": [
    {
    "label": "build main",
    "type": "shell",
    "command": "clang++",
    "args": [
    "main.cpp",
    "-o",
    "a.out",
    "-g"
    ],
    "group": {
    "kind": "build",
    "isDefault": true
    }
    },
    {
    "type": "shell",
    "label": "C/C++: clang build active file",
    "command": "/usr/bin/clang",
    "args": [
    "-g",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
    ],
    "options": {
    "cwd": "${workspaceFolder}"
    },
    "problemMatcher": [
    "$gcc"
    ],
    "group": {
    "kind": "build",
    "isDefault": true
    }
    },
    {
    "type": "cppbuild",
    "label": "C/C++: gcc 生成活动文件",
    "command": "/usr/bin/gcc",
    "args": [
    "-fdiagnostics-color=always",
    "-g",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
    ],
    "options": {
    "cwd": "${fileDirname}"
    },
    "problemMatcher": [
    "$gcc"
    ],
    "group": "build",
    "detail": "编译器: /usr/bin/gcc"
    }
    ]
    }
  • 改tasks.json如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    {
    "version": "2.0.0",
    "tasks": [
    {
    "label": "build main",
    "type": "shell",
    "command": "clang++",
    "args": [
    "main.cpp",
    "-o",
    "a.out",
    "-g"
    ],
    "group": {
    "kind": "build",
    "isDefault": true
    }
    },
    {
    "type": "shell",
    "label": "C/C++: clang build active file",
    "command": "/usr/bin/clang",
    "args": [
    "-g",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
    ],
    "options": {
    "cwd": "${workspaceFolder}"
    },
    "problemMatcher": [
    "$gcc"
    ],
    "group": {
    "kind": "build",
    "isDefault": true
    }
    },
    {
    "type": "cppbuild",
    "label": "C/C++: gcc 生成活动文件",
    "command": "/usr/bin/gcc",
    "args": [
    "-fdiagnostics-color=always",
    "-g",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
    ],
    "options": {
    "cwd": "${fileDirname}"
    },
    "problemMatcher": [
    "$gcc"
    ],
    "group": "build",
    "detail": "编译器: /usr/bin/gcc"
    }
    ]
    }

    tasks.json在这里

  • 断点,对hello.c调试一下

    断点调试

    如图,成功执行并断开则为成功。

参考

(完)