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

如何使用Clang/GCC在Mac上为C/C++设置VSCode?,c++,json,macos,visual-studio-code,C++,Json,Macos,Visual Studio Code,我一直在尝试为我的Mac电脑设置Visual Studio代码,但launch.json和task.json有问题。我将使用铿锵编译器 我试图遵循microsoft的文档,但它设置了.JSON文件来编译和调试名为helloworld.c的程序,我只想配置launch.JSON和task.JSON来编译和调试我给它的任何.c/.cpp文件。我对.JSON文件没有足够的经验来了解我正在做什么或做任何有效的事情 tasks.json: { "version": "2.0.0", "tasks": [

我一直在尝试为我的Mac电脑设置Visual Studio代码,但launch.json和task.json有问题。我将使用铿锵编译器

我试图遵循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"
    }
  ]
}

我也在努力解决这个问题。我现在已经开始工作了。对于tasks.json,您只需要1个部分,如果项目中有多个cpp文件,则需要添加${fileDirname}/*.cpp。下面是my 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
}

我也在努力解决这个问题。我现在已经开始工作了。对于tasks.json,您只需要1个部分,如果项目中有多个cpp文件,则需要添加${fileDirname}/*.cpp。下面是my 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
}
发布:

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

希望帮助您启动:

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

希望能帮助你