Mac Vscode C++环境

Mac Vscode C++环境

安装vscode

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

brew install visual-studio-code

进入vscode下载C++插件

可以使用code . 打开vscode

写cpp代码,运行选择

build-and-debug-active-file

生成的tasks.json文件修改如下

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: 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": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ 生成活动文件",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++", //非常重要,如果没有这行,将不能使用C++17新标准,也不能使用C++11标准
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ]
}

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

shift+cmd+p 搜索edit configurations(UI) 找到c_cpp_properties.json 文件如下:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

若想自定义代码风格颜色,cmd+,打开设置界面,搜索主题,找到 setting.json,打开后加上

 // 自定义的颜色
    "editor.tokenColorCustomizations": {


        "comments": "#97999d", // 注释
        "keywords": "#f063a9", // 关键字
        "variables": "#3fc2be", // 变量名 
        "strings": "#e52f66", // 字符串
        "functions": "#34bcd7", // 内置函数名
        "numbers": "#c0c06a", // 数字
        "types": "#34a0e7", //类定义颜色
    }

彻底删除vscode

sudo rm -rf $HOME/Library/Application\ Support/Code
sudo rm -rf $HOME/.vscode

之后删除vscode程序即可。

问题1: vscode无法编译 vector{} 初始化,也不能使用C++17新特性,因为tasks.json是使用cppbuild方式,在args参数中加入

“-std=c++17”,

“-stdlib=libc++”,

#include <iostream>
#include <vector>
#include <string>
#include <typeinfo>
#include <typeindex>

using namespace std;

tuple<int, double> func()
{
    return std::tuple(1, 2.2);
}

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

    auto [i, d] = func(); // 是C++11的tie吗?更高级
    cout << i << endl;
    cout << d << endl;

    cout << endl;
}

vscode终端无法输入

方法一

  1. 在tasks.json下增加
{
   "label": "Open Terminal",
   "type": "shell",
   "command": "osascript -e 'tell application \"Terminal\"\ndo script \"echo hello\"\nend tell'",
   "problemMatcher": []
 }
  1. 点击菜单栏的terminal选项下的run task ,在跳出界面选择 open terminal

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r4i5Z7aD-1685721071229)(/Users/samuelDai/Desktop/截屏2023-06-02 22.29.52.png)]

  2. 修改“launch.json”文件中的"externalConsole"的值为“true”

    之后输入时会跳出终端进行输入。

​ 参考Mac VSCode安装

方法二

  1. 在vscode中安装“codelldb”插件。安装完成后会提示你要下载一个什么平台文件(xx platform啥的),等待下载完成。

  2. 到install from VSIX 选择下载好的codelldb-aarch64-darwin.vsix 文件(这个文件m1芯片的)

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y2PgRuwv-1685721071230)(/Users/samuelDai/Desktop/截屏2023-06-02 23.13.34.png)]

  3. 修改launch.json文件为

    {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "C/C++: clang++ build and debug active file",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "C/C++: clang++ build active file"
          }
        ]
    }
    
  4. 一定要按fn+f5执行才能在内置终端输入。

​ 之后所有的输出内容均在终端显示了。

注意:

  1. 想要使用调试控制台就需要将launch.json文件改为如下,并且将codeLLdb禁用;

  2. 想要终端输入就只能使用codeLLdb,launch.json文件如上,并按fn+f5调试,所有输入输出均在终端,若想使用调试控制台,直接点击界面的三角按钮运行。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值