【问题标题】:How do I set up VSCode for C/C++ on Mac with Clang/GCC?如何使用 Clang/GCC 在 Mac 上为 C/C++ 设置 VSCode?
【发布时间】:2020-06-02 22:53:35
【问题描述】:

我一直在尝试为我的 Mac 设置 Visual Studios Code,但我遇到了 launch.json 和 task.json 的问题。我将使用 Clang 编译器。

我尝试按照 microsoft 的文档进行操作,但它设置了 .JSON 文件来编译和调试名为 helloworld.c 的程序,我只想配置 launch.json 和 task.json 来构建和调试任何 .c/ .cpp 文件我给它。我对 .JSON 文件没有足够的经验,无法知道我在做什么或做任何有效的事情。

tasks.json:

{
"version": "2.0.0",
"tasks": [
    {
        "label": "clang++ build active file",
        "type": "shell",
        "command": "clang++",
        "options": {
            "cwd": "${workspaceRoot}"
        },
        "group": "build",
        "presentation": {
            "echo": true,
            "reveal": "always",
            "focus": false,
            "panel": "shared"
        },
        "args": [
            "-std=c++17",
            "-stdlib=libc++",
            "--debug"
        ],
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": [
                "absolute"
            ],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    },
    {
        "type": "shell",
        "label": "clang build active file",
        "command": "/usr/bin/clang",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

launch.json:

  "version": "0.2.0",
  "configurations": [
    {
      "name": "clang build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "clang build",
      "miDebuggerPath": "/usr/bin/lldb"
    }
  ]
}

【问题讨论】:

    标签: c++ json macos visual-studio-code


    【解决方案1】:

    我也在为此苦苦挣扎。我现在已经开始工作了。对于tasks.json,您只需要1 个部分,如果项目中有多个cpp 文件,则需要添加“${fileDirname}/*.cpp”。这是我的 tasks.json 的样子:

    {
      "version": "2.0.0",
      "tasks": [
        {
          "type": "shell",
          "label": "clang++ build active file",
          "command": "/usr/bin/clang++",
          "args": [
            "-std=c++17",
            "-stdlib=libc++",
            "-g",
            "${fileDirname}/*.cpp",
            // "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "options": {
            "cwd": "${workspaceFolder}"
          },
          "problemMatcher": ["$gcc"],
          "group": {
            "kind": "build",
            "isDefault": true
          }
        }
      ]
    }
    

    另外,我遇​​到了矢量问题;来自 vscode 示例的错误。如果您遇到同样的问题,请尝试在 .vscode 目录中添加一个 c_cpp_properties.json 文件,如下所示:

    {
        "configurations": [
            {
                "name": "Mac",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [
                ],
                "macFrameworkPath": [
                    "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
                ],
                "compilerPath": "/usr/bin/clang",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "clang-x64"
            }
        ],
        "version": 4
    }
    

    【讨论】:

      【解决方案2】:

      启动:

      {
        "version": "0.2.0",
        "configurations": [
          {
            // MacOS
            "name": "Launch Program(lldb)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/no1_2",
            "args": [
              "4",
              "3",
              "2",
              "1"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb"
          }
      

      任务:

      {
          // See https://go.microsoft.com/fwlink/?LinkId=733558
          // for the documentation about the tasks.json format
          "version": "2.0.0",
          "tasks": [
            {
              "type": "shell",
              "label": "Build with Clang",
              "command": "/usr/bin/clang++",
              "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "${file}",
                "-o",
                "1.out",
                "-debug",
              ],
              "group": {
                "kind": "build",
                "isDefault": true
              }
            }
          ]
        }
      

      c_cpp:

      {
          "configurations": [
              {
                  "name": "Mac",
                  "includePath": ["${workspaceFolder}/**"],
                  "defines": [],
                  "macFrameworkPath": [           "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
                  ],
                  "compilerPath": "/usr/bin/clang",
                  "cStandard": "c11",
                  "cppStandard": "c++17",
                  "intelliSenseMode": "clang-x64"
              }
          ],
          "version": 4
      }
      
      

      希望能帮到你

      【讨论】:

        猜你喜欢
        • 2017-07-31
        • 2012-12-07
        • 2018-04-17
        • 1970-01-01
        • 2020-01-14
        • 2017-07-07
        • 2021-10-28
        • 1970-01-01
        • 2014-08-14
        相关资源
        最近更新 更多