Macbook Pro 安装vscode并配置c/c++环境

配置环境:Macbook Pro操作系统MacOS Catalina 10.15.4.

首先在浏览器中搜索vscode,打开搜索结果第一条,然后点击“Download for Mac”然后在页面停留一会就下载了vscode安装程序

解压缩,

将文件拉入应用程序即可。

安装插件C\C++,C\C++ Clang Command Adapter,CodeLLDB(用来debug,解决Catalina不支持lldb调试问题)以及code runner(用来编译)。

添加配置文件:tasks.json、launch.json、以及c_cpp_properties.json.

shift+command+P点击C/C++ 编辑配置(JSON),出现文件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": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

shift+command+B一直点击直到other,如下配置tasks.json文件。

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "Build with Clang",
        "type": "shell",
        "command": "clang++",
        "args": [
          "${file}",
                "-std=c++11",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out",
                "-g",
                "--debug"
        ],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

创建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": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

创建测试代码文件test.cpp

#include <iostream>
int main()
{
    for(int i=0;i<5;i++){
    std::cout<<"hey you"<<std::endl;
    }
   
    return 0;

}

点击runcode 小三角运行结果:

但是假如在for循环输出语句设置断点,调试控制台出现错误:

Warning: Debuggee TargetArchitecture not detected, assuming x86_64.

解决办法:

将之前的launch.json文件删除,添加lldb 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": [
     
       {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

设置断点:点击运行,按F5错误消失,并且按照每一步输出结果:

另外需要注意的一点:如果更改了程序的内容,保存之后,需要重新shift+command+B,产生新的.out文件,这样再按F5调试才是修改后的结果,否则仍然是修改前的结果。

参考链接:

https://www.bilibili.com/video/BV1U741157Rd/?p=2&t=40

https://www.bilibili.com/video/BV1z7411N7Pg?from=search&seid=13295932737773533081

希望对大家有帮助。

猜你喜欢

转载自blog.csdn.net/neptune4751/article/details/105925558