简体   繁体   English

如何使用 Clang/GCC 在 Mac 上为 C/C++ 设置 VSCode?

[英]How do I set up VSCode for C/C++ on Mac with Clang/GCC?

I have been trying to setup Visual Studios Code for my Mac and I am having issues with the launch.json and the task.json.我一直在尝试为我的 Mac 设置 Visual Studios Code,但我遇到了 launch.json 和 task.json 的问题。 I will be using the Clang compiler.我将使用 Clang 编译器。

I tried following microsoft's documentation, but it sets up the .JSON files to compile and debug a program named helloworld.c, I just want to configure the launch.json and the task.json to build and debug any .c/.cpp file I give it.我尝试按照 microsoft 的文档进行操作,但它设置了 .JSON 文件来编译和调试名为 helloworld.c 的程序,我只想配置 launch.json 和 task.json 来构建和调试任何 .c/.cpp 文件我给它。 I am not experienced enough with .JSON files to know what I am doing or do anything that works.我对 .JSON 文件没有足够的经验,无法知道我在做什么或做任何有效的事情。

tasks.json:任务.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:启动.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"
    }
  ]
}

I was struggling with this as well.我也为此苦苦挣扎。 I have gotten it work now.我现在已经开始工作了。 For the tasks.json you only need 1 section, and if you will have multiple cpp files in the project then you will need to add "${fileDirname}/*.cpp".对于tasks.json,您只需要1 个部分,如果项目中有多个cpp 文件,则需要添加“${fileDirname}/*.cpp”。 Here is what my tasks.json look like:这是我的 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
      }
    }
  ]
}

Also, I was having a problem with the vector ;另外,我在使用 vector 时遇到了问题; error from the vscode example. vscode 示例中的错误。 If you are running into the same problem, try to add a c_cpp_properties.json file in .vscode directory as follows:如果您遇到同样的问题,请尝试在 .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
}

launch:发射:

{
  "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"
    }

tasks:任务:

{
    // 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: 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
}

wish to help you希望帮助你

If you just want to compile or debug only any one C/C++ file in VSCode on mac,如果您只想在 mac 上的 VSCode 中编译或调试任何one C/C++ file

I highly recommend you :我强烈推荐你:

  1. Setup VSCode Extensions environment ; 设置 VSCode 扩展环境

  2. Config C/C++ compile and debug VSCode-files . 配置 C/C++ 编译和调试 VSCode-files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM